Search code examples
dockergitlabgitlab-ciplaywrightplaywright-python

Playwright container cannot reach web service container in GitLab CI/CD


I am trying to run Playwright tests within a GitLab CI/CD pipeline against a web service running in a Docker container. My gitlab-ci.yml looks like this:

stages:
  - test
services:
  - docker:27-dind

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

container-tests:
  stage: test
  when: always
  image: docker:27
  script:
    - docker run -d --name app -p 8000:80 image-name
    - curl http://docker:8000 # This works!
    - >
      docker run --name tests -v "$(pwd)/tests:/tests" 
      mcr.microsoft.com/playwright/python:v1.50.0-noble /bin/sh -c "
      pip install pytest-base-url pytest-playwright playwright &&
      playwright install &&
      pytest /tests --base-url='http://docker:8000'"
    # This doesn't work!

The web service (app container) starts and is accessible via curl http://docker:8000. However, when I try to run Playwright tests from another container, it fails to reach the service using http://docker:8000.

except Exception as error:
>           raise rewrite_error(error, f"{parsed_st['apiName']}: {error}") from None
E           playwright._impl._errors.Error: Page.goto: net::ERR_NAME_NOT_RESOLVED at http://docker:8000/
E           Call log:
E             - navigating to "http://docker:8000/", waiting until "load"

Is this the correct approach, or am I doing something fundamentally wrong? How can I properly configure the Playwright container so that it can connect to the web service container within the GitLab CI/CD pipeline?

Any guidance or best practices would be greatly appreciated!


Solution

  • In the meantime, I found a solution to my question. I needed to add my app container to the services with an alias to establish a connection. While executing commands inside this container is not possible, the app container is successfully serving content and is accessible by the Playwright container.

    container-tests:
      stage: test
      when: always
      image: mcr.microsoft.com/playwright/python:v1.50.0-noble
      services:
        - name: ${CI_REGISTRY_IMAGE}
          alias: app
      script:
        - pip install pytest-base-url pytest-playwright playwright
        - playwright install
        - pytest tests --base-url='http://app'