68

docker 容器内访问局域网服务

 2 years ago
source link: https://surest.cn/archives/191/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

docker 容器内访问局域网服务

Published on Oct 22, 2021 in with 0 comment

docker 容器内访问局域网服务

首先,我们的安装环境为 docker-compose

使用的网络模式为 bridge, 即 桥接模式

bridge

Docker网络bridge桥接模式,是创建和运行容器时默认模式。这种模式会为每个容器分配一个独立的网卡,桥接到默认或指定的bridge上,同一个Bridge下的容器下可以互相通信的。

Docker安装时会创建一个名为docker0的虚拟网桥。除非我们进行另外的配置,新创建的容器都会自动连接到这个虚拟网桥提供的风格,bridge网络用于同一主机上的docker容器相互通信,连接到同一个网桥的docker容器可以相互通信。

bridge 对宿主机来讲相当于一个单独的网卡设备 对于运行在宿主机上的每个容器来说相当于一个交换机,所有容器的虚拟网线的一端都连接到docker0上。

容器通过本地主机进行上网,容器会创建名为veth的虚拟网卡,网卡一端连接到docker0网桥,另一端连接容器,容器就可以通过网桥通过分配的IP地址进行上网。

我们也可以自定义自己的bridge网络,> docker文档建议使用自定义bridge网络,

具体不细说,参考: https://baijiahao.baidu.com/s?id=1670814302674121397&wfr=spider&for=pc

https://github.com/surest-sky/docker-compose-baota/blob/master/docker-compose.yml

我们这里构建了,两个容器

  • bt :宝塔面板
  • redis : Redis 服务

如何在宝塔面板中使用 reids 服务 ?

参考我的 Laravel env 配置

REDIS_HOST=redis // 写 redis 即可, 这里的redis 为 yml 中定义的容器名称
REDIS_PASSWORD=(null)
REDIS_PORT=6379
REDIS_DB=7

Cli 模式下 使用 Redis 无法连接

在 Cli 模式下的时候,是无法正常使用 redis 连接到我们的 redis 服务中的

从 yml 中的配置可见,我们对外暴露了 3306 端口,而且我们构建了 bridge网络,我们直接可以 使用 127.0.0.1:3306 进行访问

如何访问主机当前局域网的服务

我们在局域网中另外一台机器中 配置了一个 mysql, 已知 Mysql,域名为 192.168.50.122

我们可以先尝试 ping 一下

[root@e21ff9e5e24a wwwroot]# ping 192.168.50.122
PING 192.168.50.122 (192.168.50.122) 56(84) bytes of data.
64 bytes from 192.168.50.122: icmp_seq=1 ttl=37 time=126 ms
64 bytes from 192.168.50.122: icmp_seq=2 ttl=37 time=44.5 ms
64 bytes from 192.168.50.122: icmp_seq=3 ttl=37 time=63.4 ms
64 bytes from 192.168.50.122: icmp_seq=4 ttl=37 time=7.30 ms

是可以正常连接的

我们尝试在 Cli 模式下连接 这个 Mysql

[root@e21ff9e5e24a wwwroot]# mysql -uroot -h192.168.50.122 -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 550
Server version: 5.7.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+-----------------------------+
| Database                    |
+-----------------------------+
| information_schema          |
| default                     |

服务正常!

我们在尝试在 php-fpm 模式下访问

SQLSTATE[HY000] [1045] Access denied for user 'root'@'172.19.0.1' (using password: YES)

你会发现他的 host 变成了 172.19.0.1

关于这块因为之前不是很了解 桥接模式 和 Host 模式的区别

其实,我还是不是很明白为什么 在 cli 模式下可以连接mysql ,而使用 php-fpm 却不可以...

最后试了一下,是 mysql 密码错误...

尝试 Mysql Proxy 可以解决类似问题

dowload: https://downloads.mysql.com/archives/proxy/

cd www/wwwroot/backup/
wget https://downloads.mysql.com/archives/proxy/#:~:text=11.3M-,Download,-(mysql-proxy-0.8.5
tar -zxvf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
mv mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit mysql-proxy
vi /www/wwwroot/backup/mysql-proxy/config/mysql-proxy.cnf // 这个是我本地的目录

# 输入
[mysql-proxy]
user=root
daemon = true
pid-file = /var/run/mysql-proxy.pid
log-file = /var/log/mysql-proxy.log
log-level = debug
max-open-files = 1024
plugins = admin,proxy
user = mysql-proxy
keepalive=true

admin-username=root
admin-password=123456
admin-lua-script=/www/wwwroot/backup/mysql-proxy/lib/mysql-proxy/lua/admin.lua

#Proxy Configuration
proxy-address = 0.0.0.0:8848
proxy-backend-addresses = 192.168.50.122:3306

# 运行命令
/www/wwwroot/backup/mysql-proxy/bin/mysql-proxy --defaults-file=/www/wwwroot/backup/mysql-proxy/config/mysql-proxy.cnf

netstat -tunlp | grep 8848
ps aux | grep mysql-proxy

本文由 邓尘锋 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Oct 22, 2021 at 04:53 pm


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK