Cannot connect to mysql Docker container in GitLab CI while it works locally

To load a MySQL-Dump into a database which is running in Docker, configured with the following docker-compose file

version: '3.9'
services:
  database:
    image: registry.var-lab.com/mysql/mysql:8.0.28
    environment:
      MYSQL_ROOT_PASSWORD: symfony
      MYSQL_USER: symfony
      MYSQL_PASSWORD: symfony
      MYSQL_DATABASE: symfony
    command:
      - '--default-authentication-plugin=mysql_native_password'
      - '--sql_mode='
      - '--max_allowed_packet=1073741824'
    volumes:
      - database:/var/lib/mysql
    healthcheck:
      test: [ "CMD", "mysqladmin" ,"ping", "-h", "-u", "symfony", "-p", "symfony", "localhost" ]
      timeout: 20s
      retries: 10
    ports:
      - '3306:3306'

   [... other services/containers ...]


volumes:
  database:
    driver: local

I am using the following command:

docker-compose exec -T database mysql -usymfony -psymfony symfony < ci/db_dump.sql

The command works on my local machine where I use exactly the same docker-compose configuration.

But when I run run the same command in the GitLab CI with the following configuration

variables:
  DOCKER_DRIVER: overlay2
  CI_REGISTRY: git.xxx.de:5050

test_run:
  services:
    - docker:dind
  script:
    - apk add docker-compose git
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@git.xxx.de/xxx/testfiles.git testfiles
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker login -u reguser -p $VAR_LAB_REGISTRY_PASSWD registry.my-secred-domain.com
    # copy test files
    - mkdir -p files/import/fields && mkdir -p files/import/xxxx/data
    - cp testfiles/xxx/* files/import/fields
    - cp testfiles/xxx/* files/import/roadsurfer/data
    # done
    - docker-compose pull
    - docker-compose up -d
    - docker-compose logs
    - docker-compose exec -T database mysql -usymfony -psymfony symfony < ci/db_dump.sql

the command failes with this error message:

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

This is a bit of a random guess, but I wonder whether you need to add sleep 5 after docker-compose up -d just to make sure that everything has some time to spin up before you run the exec command?

1 Like

sleep 5 is not relevant for the question. It can be ignored. The behavior is identical when removed.