I have an Express.js application that takes input from the terminal and outputs data to the terminal interactively. For data storage, I am using PostgreSQL. The application needs to run interactively and should only start after the PostgreSQL container is properly running. I am using Docker Compose to manage the containers.
However, I am facing issues with the order of running containers. It seems like the Express.js app container is trying to start before the PostgreSQL container is ready, causing connection errors. Or it doesn’t work interactively.
How can I configure my docker-compose.yml file to ensure that the Express.js application waits for the PostgreSQL container to be fully operational before starting and it works interactively on terminal?
here is my docker-compose.yml file:
services:
postgresdb:
image: "postgres:latest"
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
- POSTGRES_DB=userinfo
container_name: postgresqldb
healthcheck:
test: ["CMD-SHELL", "pg_isready -U root -d userinfo"]
timeout: 20s
retries: 10
app:
build: .
depends_on:
postgresdb:
condition: service_completed_successfully
stdin_open: true
tty: true
My Dockerfile:
FROM node:20
WORKDIR /myapp
COPY . .
RUN npm install
EXPOSE 3000
CMD [ "npm", "start" ]
If you think the code needs to be written in this question I will also add the code.