[英]Mysql connect host change every time in the docker container,why?
In the docker container.I try to login the host mysql server. 在docker容器中。我尝试登录主机mysql服务器。 First the host ip is changed,so confused for me.But second login success.
首先,主机IP更改了,所以让我感到困惑。但是第二次登录成功。 Anyone can explain this strange happening?
任何人都可以解释这种奇怪的情况吗?
I login ip is 192.168.100.164,but the error info shows ip 172.18.0.4,which is the container localhost. 我登录的IP是192.168.100.164,但错误信息显示IP 172.18.0.4,这是容器localhost。
More info: 更多信息:
root@b67c39311dbb:~/project# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 172.18.0.1 0.0.0.0 UG 0 0 0 eth0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
root@b67c39311dbb:~/project# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.0.4 netmask 255.255.0.0 broadcast 172.18.255.255
ether 02:42:ac:12:00:04 txqueuelen 0 (Ethernet)
RX packets 2099 bytes 2414555 (2.4 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1752 bytes 132863 (132.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 35 bytes 3216 (3.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 35 bytes 3216 (3.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Try adding --add-host="localhost:192.168.100.164"
when launching docker run
. 在启动
docker run
时尝试添加--add-host="localhost:192.168.100.164"
。 But this is not a good practice to my mind. 但这对我来说不是一个好习惯。 You should move your mysql database to another container and create a network between them
您应该将mysql数据库移至另一个容器并在它们之间创建网络
That is true, when we start docker container it is get their own ip for container. 没错,当我们启动docker容器时,它会获得自己的容器IP。 You need to map host port with docker container.
您需要使用docker容器映射主机端口。 then, when you try to connect host port it redirect to myssql docker container.
然后,当您尝试连接主机端口时,它将重定向到myssql docker容器。 Please look at https://docs.docker.com/config/containers/container-networking/
请查看https://docs.docker.com/config/containers/container-networking/
I'd suggest you to create a docker bridged network and then create your container using the --add-host as suggested by Alexey. 我建议您创建一个docker bridged网络,然后使用Alexey建议的--add-host创建容器。
In a simple script: 在一个简单的脚本中:
DOCKER_NET_NAME='the_docker_network_name'
DOCKER_GATEWAY='172.100.0.1'
docker network create \
--driver bridge \
--subnet=172.100.0.0/16 \
--gateway=$DOCKER_GATEWAY \
$DOCKER_NET_NAME
docker run -d \
--add-host db.onthehost:$DOCKER_GATEWAY \
--restart=always \
--network=$DOCKER_NET_NAME \
--name=magicContainerName \
yourImage:latest
EDIT: creating a network will also simplify the communication among containers (if you plan to have it in the future) since you'll be able to use the container names instead of their IP. 编辑:创建网络还将简化容器之间的通信(如果您打算将来使用它),因为您将能够使用容器名称而不是其IP。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.