Docker containers unable to connect to each other when using gitlab runner

Good evening, i’m trying to run tests of my django application using a docker in docker runner configured as follow:

sudo gitlab-runner register -n \
  --url https://gitlab.com/ \
  --registration-token fRL4hgdxZyEKw-u2yofb \
  --executor docker \
  --description "assignment1-pss-dind" \
  --docker-image "docker:stable" \
  --docker-privileged \
  --docker-network-mode bridge

My docker-compose file:

version: '3'

services:

  djangoapp:
    image: assignment1-pss:latest
    build:
      context: .
      dockerfile: DockerFile
    volumes:
      - .:/opt/services/assignment1/src
      - static_volume:/opt/services/assignment1/static  # <-- bind the static volume
      - media_volume:/opt/services/assignment1/media  # <-- bind the media volume
    networks:  # <-- here
      - nginx_network
      - database1_network  # <-- connect to the bridge
    depends_on:  # <-- wait for db to be "ready" before starting the app
      - database1

  nginx:
    image: nginx:latest
    ports:
      - 8000:80
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - static_volume:/opt/services/assignment1/static  # <-- bind the static volume
      - media_volume:/opt/services/assignment1/media  # <-- bind the media volume
    depends_on:
      - djangoapp
    networks:  # <-- here
      - nginx_network

  database1:  # <-- IMPORTANT: same name as in DATABASES setting, otherwise Django won't find the database!
    image: postgres:10
    env_file:  # <-- we udockerse the previously defined values
      - config/db/database1_env
    networks:  # <-- connect to the bridge
      - database1_network
    volumes:
      - database1_volume:/var/lib/postgresql/data

networks:
  nginx_network:
    driver: bridge
  database1_network:  # <-- add the bridge
    driver: bridge

volumes:
  database1_volume:
  static_volume:  # <-- declare the static volume
  media_volume:  # <-- declare the media volume

My gitlab-ci file:

image: tiangolo/docker-with-compose:latest

variables:
  DOCKER_HOST: tcp://docker:2375/
  DOCKER_DRIVER: overlay2

services:
  - docker:dind

before_script:
  - docker info

build:
  stage: build
  script:
    - docker-compose build
    - docker-compose up -d
    - docker-compose exec -T djangoapp sh -c "cd assignment1_pss && python manage.py test polls"
    - docker-compose down -v

When i launch locally the command

 docker-compose exec -T djangoapp sh -c "cd assignment1_pss && python manage.py test polls"

everything works fine, but inside the runner the networks between each containers doesn’t work so when i launch the test it fails to connect to the database.
It has worked only once as it visible from the first screenshot, but now every time i make a commit i get a db connection failure…

1 Like

having the same problem. Did you ever figure this out?