Search code examples
dockerdocker-composedockerfilenestjsdevops

Nestjs Project Exiting with "exited with code 0" in docker container


I have a nestjs fresh project. I am trying to dockerize it with my Postgres database. I am not sure why my nestjs docker container is exiting with code 0.

Dockerfile

FROM node:18

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD [ "npm", "run", "start:dev" ]

docker-compose.yml

version: '3'
services:
  postgres:
    image: postgres:latest
    container_name: fda-db
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: mysecretpassword
      POSTGRES_DB: fda 
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data
    networks:
      - mynetwork  

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: fda
    depends_on:
      - postgres
    ports:
      - "3000:3000" 
    environment:
      POSTGRES_HOST: postgres
      POSTGRES_PORT: 5432
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: mysecretpassword
      POSTGRES_DB: fda 
    networks:
      - mynetwork  

volumes:
  postgres-data:

networks:
  mynetwork:  
    driver: bridge 

Error:

Attaching to fda, fda-db
fda-db  | 
fda-db  | PostgreSQL Database directory appears to contain a database; Skipping initialization
fda-db  |
fda-db  |
fda-db  | 2023-09-03 23:45:56.209 UTC [1] LOG:  starting PostgreSQL 15.4 (Debian 15.4-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
fda-db  | 2023-09-03 23:45:56.209 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
fda-db  | 2023-09-03 23:45:56.209 UTC [1] LOG:  listening on IPv6 address "::", port 5432
fda-db  | 2023-09-03 23:45:56.215 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
fda-db  | 2023-09-03 23:45:56.223 UTC [29] LOG:  database system was shut down at 2023-09-03 23:45:34 UTC
fda-db  | 2023-09-03 23:45:56.228 UTC [1] LOG:  database system is ready to accept connections
fda     | 
fda     | > food-delivery-aggregator@0.0.1 start:dev
fda     | > nest start --watch
fda     |
fda     |
fda exited with code 0

The postgres container runs well. But the fda/nestjs container exits right after it starts. I can't get through this. I tried docker system prune -a bunch of times. But it did not help. Any help from this community is highly appreciated.


Solution

  • For visibility - I will also post an answer here that has resolved the issue from the comment of the question for anyone else with the same problem

    Have you tried adding volumes to your fda service in docker-compose?

    app:
        build:
          context: .
          dockerfile: Dockerfile
        container_name: fda
        depends_on:
          - postgres
        ports:
          - "3000:3000" 
        environment:
          POSTGRES_HOST: postgres
          POSTGRES_PORT: 5432
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: mysecretpassword
          POSTGRES_DB: fda 
        networks:
          - mynetwork
        volumes: 
            - .:/app 
            - /app/node_modules