Connection issues between docker containers

docker

#1

I’m runnning MySQL 5.6 in a separate container and since yesteday (Saturday) random failures started happening: ‘SQLSTATE[HY000] [2002] Connection refused’

They are really random, I run a rebuild and everything is ok sometimes, from time to time I have to ‘rebuild without cache’ which helps more often…

Today, they are also much more often than yesteday, now it’s at 70-80% of both builds & rebuilds


#2

It’s possible MySQL is still booting and you’re hitting a race condition. How fast are your builds failing? Can you link me to some examples?


#3

The official mysql / mariadb images will create initial databases when they’re run and aren’t immediately open to connections (in fact they seem to take around 20 seconds to come online).

For me the best solution was to use my own image with pre-created data. You can easily create the empty DB files by running the official image locally with -v /myemptydb:/var/lib/mysql, then you can extend the official image, COPYing the contents of /myemptydb to /var/lib/mysql. If that directory is properly populated the server should become available immediately.

Feel free to try with mine from https://hub.docker.com/r/rh389/mysql-empty, which (for now - no guarantees) is stock 5.7 with an empty root password.


#4

Alternatively, you can wait for the container(s) to start accepting connections.

Here is an example using dockerize to wait for both MySQL and Redis connections:

$ dockerize -wait tcp://:3306 -wait tcp://:6379 -timeout 1m
2017/02/23 11:56:55 Waiting for host: :3306
2017/02/23 11:56:55 Waiting for host: :6379
2017/02/23 11:56:55 Problem with dial: dial tcp :3306: getsockopt: connection refused. Sleeping 5s
2017/02/23 11:56:55 Connected to tcp://:6379
2017/02/23 11:57:00 Connected to tcp://:3306

#5

Thanks for this suggestion. Dockerize is a great way to solve this issue. I wrote up an example: Prevent race conditions by waiting for services with Dockerize


#6

I used command nc -z 127.0.0.1 3306, It’s worked.


#7