1

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:

PORT     STATE    SERVICE  
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?

2

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.

  • Very good post, although the Idea here is for the container to access the other one through the rerouted port on the host. The containers can see themselves and inputting the IP address from the docker network they are in works. – Mario Stoilov Feb 6 '18 at 9:21
  • Okay, so you don't want container to talk to container, you want the container to go via the host to then talk to the other container? One other suggestion I have is to tail the logs on the MySQL box and see whether this gives anything away. If it doesn't show anything in the logs, then this is down to a IP/routing issue. Not entirely sure why you wouldn't want the container talking directly to the container. – Alex Johnston Feb 6 '18 at 10:13
  • I figured as much too. It's not that I don't want them to talk directly, it's just a corner case. Usually these containers are on separate machines (where everything works) and I want to use the same configuration if they were on one machine. – Mario Stoilov Feb 6 '18 at 11:28
0

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.