Home > Mobile >  docker-compose postgres and nodejs
docker-compose postgres and nodejs

Time:05-22

I would like to create a docker-compose that launches 2 containers: one my NodeJS API and the other a Postgresql DB. But I don't want the database to be accessible through the internet but only the API because only the API should contact it and no one else. I created a network and didn't publish the DB port (5432) but when I try to run my docker-compose I get an error which I don't understand.

Here is the error during docker-compose up : enter image description here

My Dockerfile :

FROM node:16.15.0
WORKDIR /app
COPY ["package.json", "package-lock.json*", "./"]
RUN npm install
COPY . .
EXPOSE 3001
CMD npm start

And my docker-compose :

version: "3"

services:
    api_nodejs:
        build: .
        depends_on:
            - db
        ports:
            - ${APP_PORT}:3001
        command: npm start
        volumes:
            - .:/app/
            - /app/node_modules
        networks:
            my_network:
                ipv4_address: 192.168.0.1

    db:
        image: postgres:14.3
        restart: always
        environment:
            - POSTGRES_DB=${DB_NAME}
            - POSTGRES_USER=${DB_USERNAME}
            - POSTGRES_PASSWORD=${DB_PASSWORD}
        networks:
            my_network:
                ipv4_address: 192.168.0.2

networks:
    my_network:
        ipam:
            driver: default
            config:
                - subnet: 192.168.0.0/24

CodePudding user response:

Your Error "Address already in use"

you give your container api_nodejs the IP 192.168.0.1, this IP is mostly used by your router.

  • Related