yen
yen

Reputation: 2292

github workflow: "ECONNREFUSED 127.0.0.1:***" error when connecting to docker container

In my github actions workflow I am getting this error (ECONNREFUSED) while running my jest test script. The test uses axios to connect to my api which is running in a container bootstrapped via docker-compose (created during the github workflow itself). That network has just has 2 containers: the api, and postgres. So my test script is, I am assume, on the "host network" (github workflow), but it couldn't reach the docker network via the containers' mapped ports.

UPDATE 1

I've narrowed down the issue as follows. When I modified the compose file to rely on the default network (i no longer have a networks: in my compose file):

Screenshot of network description

So it looks as though the containers were never attached to the default bridge network.

UPDATE 2

It looks like I just have the wrong paradigm. After reading this: https://help.github.com/en/actions/configuring-and-managing-workflows/about-service-containers I realise this is not how GA expects us to instantiate containers at all. Looks like I should be using services: nodes inside the workflow file, not using containers from my own docker-compose files. 🤔 Gonna try that...

Upvotes: 3

Views: 4256

Answers (2)

yen
yen

Reputation: 2292

So the answer is:

  1. do not use docker-compose to build your own custom containers. GA does not support this yet.
  2. Use services: in your workflow .yml file to launch your containers, which must be public docker images. If your container is based on a private image or custom dockerfile, it's not supported yet by GA.

So instead of "docker-compose up" to bootstrap postgres + my api for integration testing, I had to:

  1. Create postgres as a service container in my github workflow .yml
  2. Change my test command in package.json to:
    • first start the api as background process (because I can't create my own docker image from it 🙄) then
    • invoke my test framework next (as the foreground process)

so npm run start & npm run <test launch cmds>. This worked.

Upvotes: 4

zr0gravity7
zr0gravity7

Reputation: 3194

There are several possibilities here.

Host Network

Since you are using docker compose, when you start the api container, publish the endpoint that the api is listening on to the host machine. You can achieve this by doing:

version: 3
services:
  api:
    ...
    ports:
      - "3010:3010"

in your docker-compose.yml. This will publish the ports, similar to doing docker run ... ---publish localhost:3010:3010. See reference here: https://docs.docker.com/compose/compose-file/#ports

Compose network

By default, docker-compose will create a network called backend-net_default. Containers created by this docker-compose.yml will have access to other containers via this network. The host name to access other containers on the network is simply the name of the service. For example, your tests could access the api endpoint using the host api (assuming that is the name of your api service), e.g.:

http://api:3010

The one caveat here is that the tests must be launched in a container that is managed by that same docker-compose.yml, so that it may access the common backend-net_default network.

Upvotes: -1

Related Questions