3

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.

4
  • Post the error message... Commented May 3, 2020 at 16:37
  • 1
    Including an actual configuration that you think should work (especially the application container's configuration to reach the database) would be helpful too. One common source of trouble here is the application starting before the database; if you run docker-compose up -d, wait a minute or so, and try it a second time, do things work? Commented May 3, 2020 at 17:01
  • There is no error, it just don't do anything no response code or error from querry @madflow Commented May 3, 2020 at 17:11
  • @DavidMaze i'm launching both separated docker-compose up --build one after another, I wait until my pgadmin can connect. I'll setup this to be more powerfull. Commented May 3, 2020 at 17:12

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

In the example in the question, both containers are on a Compose-provided network named default; Networking in Compose in the Docker documentation describes this in more detail. (You don't have to explicitly name this in your docker-compose.yml file.) depends_on: is good to have but won't solve connectivity issues.
I tried and no differences with/without, problem still here.
I definitely posted this prematurely. Maybe we should first find out what the problem is. Since the reachability is formally given: Are there any errors in docker-compose logs? Also: since you are using NodeJs 14. Make sure you have pg at 8.0.3. github.com/brianc/node-postgres/issues/2180
@Wazted I updated my answer to reflect the nodejs issue.
2

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.

1 Comment

I think you didn't read the bottom part of my post and other answer/comment.
0

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

2 Comments

I did that, it's what I mean by the postgres connection url. (postgresql://[user[:password]@][netloc][:port][/dbname])
Kindly add context to any links so your answer is self contained, see "Provide context for links".

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.