GITLAB-CI - Join network created by docker-compose

Viewed 2289

I am creating a gitlab-ci to run e2e tests over my application, so, given I have this docker-compose.yml:

services:
  chrome:
    image: zenika/alpine-chrome:latest
    command: [
      chromium-browser,
      "--headless",
      "--no-sandbox",
      "--disable-gpu",
      "--ignore-certificate-errors",
      "--reduce-security-for-testing",
      "--remote-debugging-address=0.0.0.0",
      "--remote-debugging-port=9222",
      "https://google.com/",
    ]
    ports:
      - "9222:9222"
    networks:
      - test-e2e

networks:
  test-e2e:
    ipam:
      driver: default
      config:
        - subnet: 172.28.0.0/16

when I run docker-compose up everything just works fine, and on my local machine I am able to visit localhost:9222 and access the chrome debugger.


However, when I run the same job on gitlab-ci I get a ECONNREFUSED error:

F---F

Failures:

1) Scenario: List of Profiles # src/features/profile.feature:3
   ✖ Before # dist/node/development/webpack:/hooks/puppeteer.hooks.ts:17
       Failed to fetch browser webSocket url from http://localhost:9222/json/version: connect ECONNREFUSED 127.0.0.1:9222
       Error: connect ECONNREFUSED 127.0.0.1:9222
           at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)

So it is clear that I cannot join the docker-compose network and access localhost:9222 from the job

My gitlab-ci.yml is pretty straightforward and looks like this:

E2E tests:
  stage: test end-to-end
  image:
    name: docker/compose:1.24.1
    entrypoint: ["/bin/sh", "-c"]
  services:
    - docker:dind
  before_script:
    - apk --update add nodejs yarn
    - docker-compose -f test-e2e.yaml up -d
  script:
    - yarn test:cucumber
  after_script:
    - docker-compose -f test-e2e.yaml down

yarn test:cucumber basically runs cucumber and puppeteer trying to access localhost:9222 to get chrome's metadata.


How can I join the network created by docker-compose from the gitlab-ci job?

  • I don't have access to edit runner configurations
3 Answers

TL; DR On CI your chrome container is reachable using docker:9222 (or more generally <name-of-the-dind-service-on-ci>:<exposed-port>), not localhost:9222

Explanation

As per your gitlab-ci.yml, you will start 2 containers:

When running a container in Docker, the container is actually started by the Docker daemon and will run and expose ports on the host on which the Daemon is running.

  • On your machine, the Docker daemon is running locally and will expose the container ports on your local network, allowing you to reach your chrome container via localhost
  • On the CI, you are running your commands from a docker/compose:1.24.1 container but the Docker daemon is running in another container (a different host): the docker:dind container. chrome container will be created inside docker:dind container and its port exposed from this same container. You simply need to access the docker:dind container which will expose chrome ports.

By using localhost from your docker/compose:1.24.1 container, you won't be able to reach chrome because its port is not exposed on docker/compose:1.24.1 container but from docker:dind container. You need to specify its host (docker) and the exposed port (9222)

If you are using gitlab shared runners, probably you are not allowed to create networks for security reasons.

Try using your own private runners. It is really easy to set up them: https://docs.gitlab.com/runner/install/

An alternative is services.

I think good idea is to start

docker-compose up

inside CI script.

After it you will be able to access. And if you run CI again it will start same containers.

Related