简体   繁体   English

每次在docker容器中,mysql连接主机都会更改,为什么?

[英]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.

相关问题 无法从主机连接到Mysql docker容器 - Cant connect to Mysql docker container from host 从主机连接到 docker 容器中的 mysql - Connect to mysql in a docker container from the host 无法从主机连接到 Docker 容器中的 mysql - Cannot connect to mysql in Docker container from host 如何在docker容器中连接我的主机mysql? - How to connect my host mysql in docker container? Docker 容器无法连接到在主机上运行的 mysql - Docker container not able to connect to mysql running on host Docker + Airflow - 无法从 Z05B6053C41A21340AFD6FC3B234Z 容器连接到主机上的 MySQL - Docker + Airflow - Unable to connect to MySQL on host from docker container 如何从mysql工作台连接到远程主机上的mysql docker容器? - How to connect to mysql docker container on a remote host from mysql workbench? 如何将 docker 容器上的 php 脚本与网络主机连接,同时在网络主机上使用 mysql Z05B6053C41A2130AFD68BDAB3E 容器 - How connect a php script on docker container with network host, with a mysql docker container also on network host 无法将 Docker Wordpress 容器连接到主机上的 MySQL - Can't connect Docker Wordpress container to MySQL on host NodeJS Docker 容器无法连接到 MySQL(在主机上) - NodeJS Docker container can't connect to MySQL (on host)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM