1

I have MySQL 5.7 container pulled from here: 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.

However, when I try to run another container with mysql container linked like this:

docker run --link mysql:mysql -p 8080:8080 -d app:dev

my container can't connect to 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.

Here's nmap -p 3306 172.17.0.2 output:

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.

How to connect to MySQL server from another docker container?

2
  • Have you tried checking the firewall rules? I had a similar problem and ended up resolving it by doing sudo ufw allow 3306/tcp && sudo service ufw restart (Ubuntu 14.04). After that, I was able to connect by running mysql -uuser -ppwd -h192.168.1.2 -P3306 from a container.
    – Jedihe
    Jul 25, 2018 at 22:02
  • Just learned of a way to control access to port 3306 (better than allowing anybody, as in my first comment): sudo ufw allow from 172.0.0.0/8 to 192.168.1.0/24 port 3306. I'm assuming that Docker containers get IPs like 172.a.b.c, and the host IP is 192.168.1.x.
    – Jedihe
    Jul 25, 2018 at 22:50

0

You must log in to answer this question.

Browse other questions tagged .