0

I am currently working on a web-app which uses postgres and Node.js Express. I have dockerized this application and pushed it to the Artifact registry. I have pulled both the images to the VM.

My docker compose file looks like this:

services:
    postgres:
        image: postgres:latest
        container_name: postgres
        environment:
            POSTGRES_DB: web_app_db
            POSTGRES_USER: developer
            POSTGRES_PASSWORD: test1234
        ports:
            - "5432:5432"
        volumes:
            - postgres-data:/var/lib/postgresql/data
        network:
            - my-nw
    api:
        image: web-api 
        command: >
            sh -c '
                npm run upgrade-docker &&
                if [ "$SEED" = "true" ]; then
                    npm run seed-roles ./src/environments/.env.docker &&
                    npm run seed-stores ./src/environments/.env.docker &&
                    npm run seed-products ./src/environments/.env.docker
                fi &&
                npm run start ./src/environments/.env.docker
            '
        environment:
            - WORKDIR=$PWD
            - PORT=80
            - SEED=true
        volumes:
            - ./src:/app/src
            - ./dist:/app/dist
        depends_on:
            - postgres
        network:
            - my-nw
networks:
  my-nw:
    driver: bridge

volumes:
    postgres-data:

When I run docker compose up -d postgres, Postgres server starts and is up and running and it exposes port 5432. When I run docker compose up api, the web api starts but it is unable to connect to Postgres. After a while the connection times out with error:

api  | Error: connect ETIMEDOUT 172.22.0.2:5432
api  |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
api  |   errno: -110,
api  |   code: 'ETIMEDOUT',
api  |   syscall: 'connect',
api  |   address: '172.22.0.2',
api  |   port: 5432
api  | }

Is something missing ?

I have tried changing the host names from postgres to 0.0.0.0 and localhost in the api container but nothing works. I have tried using the default network instead of my-nw but that doesn't work either. I can connect to postgres from the host machine using the command psql -U developer -h localhost -p 5432 Ironically this same docker compose file works just fine on my local machine but fails on the VM.

2 Answers 2

0

is likely due to the API container being unable to connect to the database container.

If you want to run everything through Docker using docker-compose, you can reference the service name from the compose file when connecting the API to the database (Postgres).

This is because if you refer to 'localhost', it refers to the container itself, not the host machine.

For example, if the service name of the database is postgres, configure it as follows:

const pool = new Pool({
  user: 'me',
  host: 'localhost',
  database: 'api',
  password: 'password',
  port: 5432,
})
1
  • I'm not using localhost, I am using the service name "postgres"
    – Divya
    Commented May 25, 2024 at 7:16
0

The issue was with the version mismatch of docker itself. The docker compose version locally was 2.27 and on the VM was 2.30. Reinstalling docker to match the same version worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.