mysqldockercontinuous-integrationcirrus-ci

Cirrus CI - Can't connect to MySQL additional container


I'm running a container on Cirrus CI, and in my .cirrus.yml, I've defined an additional_container to run a MySQL instance to test against as per the docs:

.cirrus.yml

container:
  image: node:latest
  additional_containers:
    - name: mysql
      image: mysql:8
      port: 3306
      cpu: 1.0
      memory: 512Mi
      env:
        MYSQL_ROOT_PASSWORD: "pa55w0rd"

I'm trying to run a CREATE DATABASE command against that instance in one of my setup tasks:

...

mysql_setup_script:
    - mysql -uroot -ppa55w0rd -P3306 -hlocalhost -e "CREATE DATABASE voluble_test;"
...

I've installed the MySQL client (but not the server, as this would defeat the object) on my testing container. However, MySQL acts as if it were connecting to a true localhost DB and looks for a locally-installed MySQL server, it appears - and fails with the following error:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

That said, the Cirrus CI docs state that the MySQL instance should be available at localhost:

Tests will be able to access MySQL instance via localhost:3306.

I can't see an obvious way around this - any advice?


Solution

  • Using localhost will cause mysql not to use the network.

    Use either 127.0.0.1 or your docker host IP, depending on network configs.

    Notice that the error is not a network error:

    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
    

    "through socket '/var/run/mysqld/mysqld.sock'", that file does not point to a mysql socket inside your docker container.

    Grab your docker host IP with:

    $ docker network inspect bridge --format='{{ (index .IPAM.Config 0).Gateway}}'