Wazted
Wazted

Reputation: 101

NodeJS 14 in a Docker container can't connect to Postgres DB (in/out docker)

I'm making a React-Native app using Rest API (NodeJS, Express) and PostgreSQL.

Everything work good when hosted on my local machine. Everything work good when API is host on my machine and PostgreSQL in docker container.

But when backend and frontend is both in docker, database is reachable from all my computer in local, but not by the backend.

I'm using docker-compose.

version: '3'
services:
  wallnerbackend:
    build:
      context: ./backend/
      dockerfile: ../Dockerfiles/server.dockerfile
    ports:
    - "8080:8080"
  wallnerdatabase:
    build:
      context: .
      dockerfile: ./Dockerfiles/postgresql.dockerfile
    ports:
    - "5432:5432"
    volumes:
    - db-data:/var/lib/postgresql/data
    env_file: .env_docker
volumes:
  db-data:

.env_docker and .env have the same parameters (just name changing).

Here is my dockerfiles:

Backend

FROM node:14.1

COPY package*.json ./

RUN npm install

COPY . .

CMD ["npm", "start"]

Database

FROM postgres:alpine

COPY ./wallnerdb.sql /docker-entrypoint-initdb.d/

I tried to change my hostname in connection url to postgres by using the name of the docker, my host IP address, localhost, but no results.

It's also the same .env (file in my node repo with db_name passwd etc) I do use in local to connect my backend to the db.

Upvotes: 3

Views: 5546

Answers (3)

madflow
madflow

Reputation: 8490

Since you are using NodeJS 14 in the Docker Container - make sure that you have the latest pg dependency installed:

https://github.com/brianc/node-postgres/issues/2180

Alternatively: Downgrade to Node 12.

Also make sure, that both the database and the "backend" are in the same network. Also: the backend should best "depend" on the database.

version: '3'

services:
  wallnerbackend:
    build:
      context: ./backend/
      dockerfile: ../Dockerfiles/server.dockerfile
    ports:
      - '8080:8080'
    networks:
      - default
    depends_on:
      - wallnerdatabase
  wallnerdatabase:
    build:
      context: .
      dockerfile: ./Dockerfiles/postgresql.dockerfile
    ports:
      - '5432:5432'
    volumes:
      - db-data:/var/lib/postgresql/data
    env_file: .env_docker
    networks:
      - default

volumes:
  db-data:

networks:
  default:

This should not be necessary in you case - as pointed out in the comments - since Docker Compose already creates a default network

The container name "wallnerdatabase" is the host name of your database - if not configured otherwise.

Upvotes: 4

Mahdi AlKhalaf
Mahdi AlKhalaf

Reputation: 101

I expect the issue to be in the database connection URL since you did not share it.

Containers in the same network in a docker-compose.yml can reach each other using the service name. In your case the service name of the database is wallnerdatabase so this is the hostname that you should use in the database connection URL.

The database connection URL that you should use in your backend service should be similar to this:

postgres://user:password@wallnerdatabase:5432/dbname

Also make sure that the backend code is calling the database using the hostname wallnerdatabase as it is defined in the docker-compose.yml file.

Here is the reference on Networking in Docker Compose.

Upvotes: 2

GintsGints
GintsGints

Reputation: 855

You should access your DB using service name as hostname. Here is my working example - https://gitlab.com/gintsgints/vue-fullstack/-/blob/master/docker-compose.yml

Upvotes: 0

Related Questions