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.
GitLab CI allows you to use Docker Engine to build and test docker-based projects. This also allows to you to use docker-compose and other docker-enabled tools.
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
When you start Docker, a default bridge network (also called bridge ) is created automatically, and newly-started containers connect to it unless otherwise specified. You can also create user-defined custom bridge networks.
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:
docker/compose:1.24.1
container from which you will run docker-compose
and yarn
commandsdocker:dind
on which a Docker Daemon will run. This container will be reachable from docker/compose:1.24.1
container via hostname docker
(see GitlabCI doc on accessing services)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.
chrome
container via localhost
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With