4

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?

1
  • Apparently the problem was with my another container Commented Jun 9, 2018 at 14:43

1 Answer 1

4

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. 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.

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. curl and ping are installed in this container.

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.

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

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.

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That's how I tried as well, but the port is still closed. What mysql image are you using?
Also calling docker run --rm --network mynetwork uzyexe/nmap 172.17.0.2 3306 outputs Host seems down. 172.17.0.3 doesn't work neither. I've connected mysql to mynetwork this way: docker network connect mynetwork mysql
I'm using the official mysql image (latest tag).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.