Docker-to-docker Mysql connection

Posted on

Question :

I have a 2 docker setup – one container with a MysqlDB on it and another with a web server. Both are on the same machine.

The db container has it 3306 port exposed though the hosts 3306 port (argument -p 3306:3306). This container is reachable (I can log into the mysql database) both through the host machine and outside, but for some reason, the web server container fails to connect to it.

I have ssh on the container with the web server. When I run:

nmap -p 3306 host.machine.ip.address 

on it, I get:

3306/tcp filtered mysql

and when I actually try to log in I get:

mysql -h host.machine.ip.address -u root -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on 'host.machine.ip.address' (113)

Can somebody point me to where the issue might be?

Answer :

First of all are the containers visible to one another i.e. can you ping the mysql box from the webserver?

If not it is a visibility issue, try creating a docker network to put the two containers within.

First create a docker network:

docker network create lamp-network

You can see this by the following command:

docker network ls

Now start your two containers with the additional flag:

--net lamp-network

Now try pinging and seeing if you have connection, if so then MySQL should work, although depending on setup you may have to comment out the bind-address from the config.

Most of the time that I encountered the same issue is because of the firewall or iptables. Can you try to run service iptables save then stop the iptables then try to connect to the DB.

Leave a Reply

Your email address will not be published. Required fields are marked *