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.
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.