skip to Main Content

Below is my Dockerfile:

FROM node:14
WORKDIR /workspace
COPY . .
COPY /prisma ./prisma/
RUN npm install
EXPOSE 3333
EXPOSE 9229

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

And my docker-compose.yml

version: '3.8'
services:
  todoapp-api:
    container_name: todoapp-api
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 3333:3333

  postgres:
    image: postgres:13.5
    container_name: postgres
    restart: always
    environment:
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypassword
    volumes:
      - postgres:/var/lib/postgresql/data
    ports:
      - '5432:5432'

volumes:
  postgres:

networks:
  nestjs-crud:

And my .env:

DATABASE_URL="postgresql://myuser:[email protected]/mydb?schema=public"

After struggling with making the database run and be accessible, I found out that one possible solution was to change the DATABASE_URL. As you can see, I am writing my IP Address there to get it to run and this works for me. However, when I replace 192.168.1.1 with the name of the service: postgres, it stops working and I get the error:
Can't reach database server at postgres:5432

Writing the IP address is not ideal of course. However, if I don’t write the IP address then the database server just doesn’t work.

2

Answers


  1. I think you may need to atributte networks in the containers specs. You already defined what networks you have in the YAML but they need to be inserted in container’s spec like

    todoapp-api:
    container_name: todoapp-api
    networks:
    - nestjs-crud
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 3333:3333
    networks:
      nestjs-crud:
        internal: true
    

    My recomendation is to create one network for the db and other for the API, then assing the network db for the db, and both in the API, thus, the API can acess db network. Than, you can acess the db by the host nestjs-crud.postgres

    Login or Signup to reply.
  2. To bounce back, on the point of the comment above, the two services are not in the same network, which is why you have the concern. To solve this problem, it will be necessary to put the services in the same network by putting the mention:

    networks:
    - nestjs-crud
    

    and depends_on in todoapp-api

    in the todoapp-api and postgres service, this becomes:

    version: '3.8'
    services:
      todoapp-api:
        container_name: todoapp-api
        build:
          context: .
          dockerfile: Dockerfile
        ports:
          - 3333:3333
        networks:
          - nestjs-crud
        depends_on:
          - postgres
    
      postgres:
        image: postgres:13.5
        container_name: postgres
        restart: always
        environment:
          - POSTGRES_USER=myuser
          - POSTGRES_PASSWORD=mypassword
        volumes:
          - postgres:/var/lib/postgresql/data
        ports:
          - '5432:5432'
        networks:
          - nestjs-crud
    
    volumes:
      postgres:
    
    networks:
      nestjs-crud:
    

    And add in .env database service name.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search