Hello,
Cannot reach MySQL service when running tests using a docker container
I have this simple setup to reproduce :
simple_test:
services:
- docker:19.03.12-dind
- mysql:5.7.32
script:
- apk --no-cache add mysql-client
- mysql --version
- mysql -u root -p"$MYSQL_ROOT_PASSWORD" -h mysql "${MYSQL_DATABASE}" -e "SHOW DATABASES;"
- docker run --network host --rm imega/mysql-client mysql --host=mysql --user=root -p"$MYSQL_ROOT_PASSWORD" --database="${MYSQL_DATABASE}" --execute='show databases;'
stage: test
when:
manual
The host mysql-client can connect to the mysql service fine, but the docker container cannot event using the --network host mode. What am I missing ? can I bind the mysql host to a specific url or forward the mysql name to the container ?
Thanks a lot !
Hi @amaury.mercier
your mysql client doesn’t know what ‘mysql’ is. You are not running containers in the same Docker daemon so you cannot use container names.
In this case the host
for mysql client needs to be IP address of the mysql service container. You can get that from /etc/hosts in the build container. As an alternative you can use --add-host
as well
Something like this should work
- MYSQL_IP=$(grep 'mysql ' /etc/hosts | cut -f1 | head -n 1)
- docker run --add-host "mysql:$MYSQL_IP" --rm imega/mysql-client mysql --host=mysql --user=root -p"$MYSQL_ROOT_PASSWORD" --database="${MYSQL_DATABASE}" --execute='show databases;'
2 Likes
Hi,
This was it - this also worked for any other service (redis, rabbitmq, mongo…) that I had spun with mysql.
Thank you very much for your help !
Have a great day
On our runner /etc/hosts
did not have entries for the services so I ended up going via dig:
- apk add --no-cache bind-tools
- DATABASE_IP_ADDRESS=$(dig +short mysql)
- DATABASE_URL=mysql://root:password@$DATABASE_IP_ADDRESS:3306/my_database
Then using this URL worked with any containers