Can't connect to circleci/mysql:5.7 container on localhost or 127.0.0.1

I’m using circleci/mysql:5.7 as a secondary container, because it has the right environment variables set from the start.

The doc says all containers should be on the same network, but I can’t connect to it from my primary container (a private Debian-based image with “mysql-client” installed), and yes I’m forcing a TCP connection to avoid using the Unix socket.

mysql --protocol=tcp "SELECT 1"
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (99)
aExited with code 1

Same with MYSQL_HOST set to 127.0.0.1 in the primary container environment :

mysql --protocol=tcp "SELECT 1"
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
aExited with code 1

Any ideas very welcome

Ok, my MySQL command line syntax is wrong for one thing, but that didn’t explain the connection failure.

It seems that the MySQL container just wasn’t ready to accept connections, after sleeping for a few seconds it all works.

How do you usually solve that ? A sleep command is ugly.
Is it possible to pause the primary container until the others are ready ?

You cannot pause a container until others are ready, but I’ve done something like this in the past (for PostgreSQL) which should work for any arbitrary command as well.

There is still a sleep, but it will take as long as it needs rather than being hard coded.

  until psql -U postgres -c '\l'; do
    >&2 echo "Postgres is starting up ... will try again momentarily"
    sleep 1
  done

Thanks, that’s what I had in mind, seems good enough.

For what it’s worth if you use docker compose in production you may run into this issue as well so I usually add a similar script to the entry point of my app.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.