ECONNREFUSED when attempting to access Docker service in GitLab CI

Viewed 26

I am trying to access a Docker container which exposes an Express API (using Docker Compose services) in GitLab CI in order to run a number of tests against it.

I setup and instantiate the Docker services necessary as one task, then I attempt accessing it via axios requests in my tests. I have set 0.0.0.0 as the endpoint base.

However, I keep receiving the error:

[Error: connect ECONNREFUSED 0.0.0.0:3000]

My docker-compose.yml:

version: "3"
services:
  st-sample:
    container_name: st-sample
    image: sample
    restart: always
    build: .
    expose:
      - "3000"
    ports:
      - "3000:3000"
    links:
      - mongo
  mongo:
    container_name: mongo
    image: mongo
    volumes:
      - /sampledb
    expose:
      - "27017"
    ports:
      - "27017:27017"

My gitlab-ci.yml:

image: docker:latest

services:
  - node
  - mongo
  - docker:dind

stages:
  - prepare_image
  - setup_application
  - test
  - teardown_application

prepare_image:
  stage: prepare_image
  script:
    - docker build -t sample .

setup_application:
  stage: setup_application
  script:
    - docker-compose -f docker-compose.yml up -d

test:
  image: node:latest
  stage: test
  allow_failure: true
  before_script:
    - npm install
  script:
    - npm test

teardown_application:
  stage: teardown_application
  script:
    - docker-compose -f docker-compose.yml stop

Note that I also have registered the runner in my machine, giving it privileged permissions.

Locally everything works as expected - docker containers are initiated and are accessed for the tests.

However I am unable to do this via GitLab CI. The Docker containers build and get set up normally, however I am unable to access the exposed API.

I have tried many things, like setting the hostname for accessing the container, setting a static IP, using the container name etc, but to no success - I just keep receiving ECONNREFUSED.

I understand that they have their own network isolation strategy for security reasons, but I am just unable to expose the docker service to be tested.

Can you give an insight to this please? Thank you.

1 Answers

I finally figured this out, following 4 days of reading, searching and lots of trial and error. The job running the tests was in a different container from the ones that exposed the API and the database.

I resolved this by creating a docker network in the device the runner was on:

sudo network create mynetwork

Following that, I set the network to the docker-compose.yml file, with external config, and associated both services with it:

  st-sample:
    # ....
  networks:
    - mynetwork

  mongo:
    # ....
    networks:
      - mynetwork


networks:
  mynetwork:
    external: true

Also, I created a custom docker image including tests (name: test), and in gitlab-ci.yml, I setup the job to run it within mynetwork.

docker run --network=mynetwork test

Following that, the containers/services were accessible by their names along each other, so I was able to run tests against http://st-sample.

It was a long journey to figure it all out, but it was well-worth it - I learned a lot!

Related