2

Hello I have the following error in my node project:

(node:51) UnhandledPromiseRejectionWarning: Error: getaddrinfo ENOTFOUND ${DB_HOST}

I'm thinking the problem is that my postgress is not yet started when my project starts and so I'm not able to think of a solution on how to start my container after my postgres is ready, I read something about dockerize, but I'm not able to imagine how to apply

my docker file:

FROM node:lts-alpine

RUN mkdir -p /home/node/api/node_modules && chown -R node:node /home/node/api

WORKDIR /home/node/api

COPY ormconfig.json .env package.json yarn.* ./

USER node

RUN yarn

COPY --chown=node:node . .

EXPOSE 4000

CMD ["yarn", "dev"]

my docker compose:

version: '3.7'

services:
  ci-api:
    build: .
    container_name: ci-api
    volumes:
      - .:/home/node/api
      - /home/node/api/node_modules
    ports:
      - '${SERVER_PORT}:${SERVER_PORT}'
    depends_on:
      - ci-postgres
    networks:
      - ci-network
  ci-postgres:
    image: postgres:12
    container_name: ci-postgres
    ports:
      - '${DB_PORT}:5432'
    environment:
      - ALLOW_EMPTY_PASSWORD=no
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASS}
      - POSTGRES_DB=${DB_NAME}
    volumes:
      - ci-postgres-data:/data
    networks:
      - ci-network

volumes:
  ci-postgres-data:

networks:
  ci-network:
    driver: bridge

and this is my .env

SERVER_PORT=4000
DB_HOST=ci-postgres
DB_PORT=5432
DB_USER=spirit
DB_PASS=api
DB_NAME=emasa_ci
2

1 Answer 1

3

You can reference the below docker-compose.yml in which depends_on, healthcheck and links are added as web service depends on db service.

Reference: Postgresql Container is not running in docker-compose file - Why is this?

version: "3"
services:
  webapp:
    build: .
    container_name: webapp
    ports:
      - "5000:5000"
    links:
      - postgres
    depends_on:
      postgres:
        condition: service_healthy

  postgres:
    image: postgres:11-alpine
    container_name: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=tmp
      - POSTGRES_USER=tmp
      - POSTGRES_PASSWORD=tmp_password
    volumes:  # Persist the db data
      - database-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  database-data:
Sign up to request clarification or add additional context in comments.

Comments

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.