Can't connect to Postgres service

I have written a github action that runs tests in a docker container. Sometimes users of that action will want to connect to a database for the tests, I would expect they could just use a postgres service to do so,  but currently I cannot get that to work. 

name: On Prs
on: [pull_request]

    runs-on: ubuntu-latest

        image: postgres:11-alpine
          POSTGRES_USER: monster
          POSTGRES_PASSWORD: monster
          - "54321:5432"
        # needed because the postgres container does not provide a healthcheck
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 10

      - uses: actions/checkout@v2
      - name: "run dem tests"
        uses: Adzz/elixir_run_tests_action@v2.0.0

The app is correctly configured to listen port 54321 and the db user and password are configured correctly to both be monster. Is there something obvious I am missing??

The error is:

(DBConnection.ConnectionError) tcp connect (postgres:54321): connection refused - :econnrefused  


Hi @adzz ,

Thank you for being here!

From your code ‘ports: - “54321:5432”’, you are mapping TCP port 5432 in the container to port 54321 on the Docker host. I don’t have your exact code, so if your app is listening port on host runner, it should be ‘54321’ port.

But since you’re using a github container action to communicate with postgres service, as doc mentioned:

your step uses container actions, you don’t need to map ports to access the service or action. Docker automatically exposes all ports between containers on the same Docker user-defined bridge network. You can directly reference the service container by its hostname. The hostname is automatically mapped to the label name you configure for the service in the workflow.

You should not need to listen to the port but reference its hostname/label name directly.


Thanks for the response, that was a typo my App was configured to listen to 54321, so it should have been working.

If i drop the port mapping how will it know what port my app is configured to listen to? Indeed if i do that it (predictably) doesn’t connect to the database.

Hi @adzz,

Thanks for your reply!  You can use this snippet environment variable in the env setting,  then workflow will know which port is used for connection.

POSTGRES_PORT: ${{[5432] }}

There is  actions/example-services for using Actions with services in both a VM and Docker container. Hope it helps!