[英]Can't access MySQL database in Docker container from another container
I have MySQL 5.7 container pulled from here: https://hub.docker.com/_/mysql/ 我从这里拉了MySQL 5.7容器: https : //hub.docker.com/_/mysql/
Here's how I run it: 这是我如何运行它:
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pwd -d mysql:5.7
It works good, I'm able to connect to MySQL db from my host machine. 它运行良好,我可以从我的主机连接到MySQL数据库。
However, when I try to run another container with mysql
container linked like this: 但是,当我尝试运行另一个容器,其中
mysql
容器链接如下:
docker run --link mysql:mysql -p 8080:8080 -d app:dev
my container can't connect to mysql
: 我的容器无法连接到
mysql
:
# 172.17.0.3 is mysql's ip taken from /etc/hosts of another container.
mysql -h 172.17.0.3 -u root -ppwd
ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.3'
I tried to use docker networks but I'm getting the same error. 我试图使用docker网络,但我得到了同样的错误。
Here's nmap -p 3306 172.17.0.2
output: 这是
nmap -p 3306 172.17.0.2
输出:
Starting Nmap 7.01 ( https://nmap.org ) at 2018-06-03 08:34 UTC
Nmap scan report for e66874413058 (172.17.0.2)
Host is up (0.00012s latency).
PORT STATE SERVICE
3306/tcp closed mysql
Nmap done: 1 IP address (1 host up) scanned in 0.39 seconds
For unknown reason, the port is closed. 由于未知原因,端口已关闭。 If I run
nmap
command from my host, it's open. 如果我从我的主机运行
nmap
命令,它是打开的。
How to connect to MySQL server from another docker container? 如何从另一个docker容器连接到MySQL服务器?
I have to admit I don't see immediately where it's going wrong because also IP based communication should work but let me explain the recommended way to let containers communicate. 我不得不承认我没有立即看到它出错的地方,因为基于IP的通信也应该有效,但让我解释让容器通信的推荐方法。 When you link your app container with the mysql container (like your doing) you can access the mysql just on it's container name without using ip's.
当您将应用程序容器与mysql容器链接时(就像您正在做的那样),您可以在不使用ip的情况下访问其容器名称上的mysql。
In the default bridge network: 在默认桥接网络中:
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pwd -d mysql:5.7
Now I start a random app and link it with mysql. 现在我启动一个随机应用程序并将其与mysql链接。
curl
and ping
are installed in this container. curl
和ping
安装在此容器中。
docker run -d -p 8080:8080 --link mysql:mysql randomapp
Now I go inside my randomapp container and try to ping
the mysql container which works. 现在我进入我的randomapp容器并尝试
ping
的mysql容器。
docker exec -it 7c4bc6f1ca7a bash
xxx@7c4bc6f1ca7a:/$ ping mysql
PING mysql (172.17.0.3) 56(84) bytes of data.
64 bytes from mysql (172.17.0.3): icmp_seq=1 ttl=64 time=0.076 ms
64 bytes from mysql (172.17.0.3): icmp_seq=2 ttl=64 time=0.049 ms
I can verify with an nmap container too 我也可以使用nmap容器进行验证
docker@default:~$ docker run --rm --link mysql:mysql uzyexe/nmap mysql 3306
Starting Nmap 7.60 ( https://nmap.org ) at 2018-06-06 05:54 GMT
setup_target: failed to determine route to 3306 (0.0.12.234)
Nmap scan report for mysql (172.17.0.3)
Host is up (0.000010s latency).
Not shown: 999 closed ports
PORT STATE SERVICE
3306/tcp open mysql
MAC Address: 02:42:AC:11:00:03 (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 1.65 seconds
docker@default:~$
If you deploy your app and mysql in the same user defined bridge network you don't need to define the --link
option and your containers can talk with each other by using their container name. 如果在同一个用户定义的桥接网络中部署应用程序和mysql,则无需定义
--link
选项,并且容器可以使用其容器名称相互通信。
docker network create my-bridge
docker run --name mysql --net my-bridge -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pwd -d mysql:5.7
docker run -d -p 8080:8080 --net my-bridge randomapp
It's recommended to use user defined networks and not the 'deprecated' --link feature in the default bridge network. 建议使用用户定义的网络,而不是默认桥接网络中的“已弃用” - 链接功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.