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