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:

# is mysql's ip taken from /etc/hosts of another container.
mysql -h -u root -ppwd

ERROR 2003 (HY000): Can't connect to MySQL server on ''

I tried to use docker networks but I'm getting the same error.

Here's nmap -p 3306 output:

Starting Nmap 7.01 ( https://nmap.org ) at 2018-06-03 08:34 UTC
Nmap scan report for e66874413058 (
Host is up (0.00012s latency).
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?

  • 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 '18 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 to 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 '18 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.