I spent a whole day to this point, still struggling, the error says "Failed to connect to localhost port 9000 after".
I have a Nodejs app, which uses Postgres as DB. I was able to connect them together. And, the app runs in attach mode very well. When, I run it on de-attach mode, and curl it, I get the error. I even put a long time sleep to make sure it has enough time to start the docker but still failed to connect to the port
The main line is how I run the docker, and get Postgres as a service. I have checked the health of the service. I am not sure if this is a firewall or networking issue i.e. the interfaces here.
- docker run -d -e POSTGRES_HOST=$POSTGRES_PORT_5432_TCP_ADDR -p 9000:9000 $DOCKER_TEST_IMAGE_API
image: docker:19.03.12
stages:
- build
- test
variables:
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
DOCKER_DRIVER: overlay2
DOCKER_TEST_IMAGE_API: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
DOCKER_RELEASE_IMAGE_API: $CI_REGISTRY_IMAGE
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
api-component-api:
stage: build
services:
- docker:19.03.12-dind
variables:
PORT: '9000'
script:
- docker build --pull -t $DOCKER_TEST_IMAGE_API api/.
- docker push $
enter code here
api-component-api:
stage: build
services:
- docker:19.03.12-dind
variables:
PORT: '9000'
script:
- docker build --pull -t $DOCKER_TEST_IMAGE_API api/.
- docker push $DOCKER_TEST_IMAGE_API
api-component-tests:
stage: test
services:
- name: 'postgres:11.9'
alias: postgres
- name: 'docker:19.03.12-dind'
alias: docker
variables:
# POSTGRES Service
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'password'
POSTGRES_DB: 'postgres'
POSTGRES_HOST: 'postgres'
POSTGRES_HOST_AUTH_METHOD: trust
script:
- env | grep POSTGRES_PORT_5432_TCP_ADDR
- docker run -d -e POSTGRES_HOST=$POSTGRES_PORT_5432_TCP_ADDR -p 9000:9000 $DOCKER_TEST_IMAGE_API
- sleep 60
- docker ps -a
- docker network ls
- curl -X GET "http://localhost:9000/rooms/1000ef5c-1657-46b2-bb36-c74080e00c01"
- cd end-to-end-tests
- yarn install
- yarn test
Digest: sha256:f8d84da7264faf570184929a441e448d680ccbfd297bcd0aef0d7f455c360614
Status: Downloaded newer image for registry.gitlab.com/.../simple-room-booking:main
f24ef88e36e16beb7f32acb03f7cda5775742b6639232a9692e4ef494fe22e93
$ sleep 60
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f24ef88e36e1 registry.gitlab.com/.../simple-room-booking:main "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:9000->9000/tcp lucid_yalow
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
c7d6ee26e52e bridge bridge local
6760c4e13b56 host host local
3bb3bbf3cd42 none null local
$ curl -X GET "http://localhost:9000/rooms/1000ef5c-1657-46b2-bb36-c74080e00c01"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (7) Failed to connect to localhost port 9000 after 6 ms: Connection refused
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 7
I do not have much experience with GitLab.
I did spend two days on this. The problem was from networking between the docker.
Source: https://docs.gitlab.com/ee/ci/services/#using-services-with-docker-run-docker-in-docker-side-by-side
variables:
FF_NETWORK_PER_BUILD: "true" # activate container-to-container networking
This works after some refactoring, but the main piece was this feature flag.
api-component-tests:
stage: test
services:
- name: 'postgres:11.9'
alias: postgres
- name: 'docker:19.03.12-dind'
alias: docker
- name: $DOCKER_TEST_IMAGE_API
alias: api
variables:
# POSTGRES Service
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'password'
POSTGRES_DB: 'postgres'
POSTGRES_HOST: 'postgres'
POSTGRES_HOST_AUTH_METHOD: trust # activate container-to-container networking
# API Service
POSTGRES_DSN: 'postgresql://postgres/postgres?sslmode=disable&user=postgres&password=password'
FF_NETWORK_PER_BUILD: "true"
script:
- apk --update add postgresql-client
- apk add nodejs yarn curl
- sleep 10
- curl -X GET "http://api:9000/rooms/1000ef5c-1657-46b2-bb36-c74080e00c01"
- cd end-to-end-tests
- export apiBaseUrl='http://api:9000'
- yarn install
- yarn test