0

I am trying to dockerize a react app with postgres database.

I am new to docker, so I followed tutorials online to come up with Dockerfile and docker-compose as shown below.

Dockerfile

# pull the official base image
FROM node:13.12.0-alpine
# set working direction
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
EXPOSE 1338
ENV PATH /app/node_modules/.bin:$PATH
# install application dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm i
# add app
COPY . ./
# start app
CMD ["npm", "start"]

docker-compose.yml

version: '3.7'

services:

  sample:
    container_name: sample
    build:
      context: .
      dockerfile: ./Dockerfile
    volumes:
      - '.:/app'
      - '/app/node_modules'
    ports:
      - 1338:1338
    environment:
      - CHOKIDAR_USEPOLLING=true
      - ASPNETCORE_URLS=https://+:1338
      - ASPNETCORE_HTTPS_PORT=1338
    depends_on:
      - db
  
  db:
    container_name: db
    image: postgres:14-alpine
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: ###
      POSTGRES_USER: ###
      POSTGRES_PASSWORD: ###
      # I hide these information for privacy purpose, but I am 100% sure I input these information correctly.
    volumes:
      - ./db-data/:/var/lib/postgresql/data/

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

volumes:
  pgdata1:

so what happened is when I tried to run docker-compose up , I suppose the db part had no issue, since it wrote database system is ready to accept connections. However, the "sample" part ended up with an error:

Server wasn't able to start properly.
error Error: connect ECONNREFUSED 127.0.0.1:5432
 at TCPConnectWrap.afterConnect [as oncomplete]

which does not make much sense to me since the database is already up so there should not be any issue with connection at all.

Feel free to share your view, any idea would be appreciated. Thank you.

Anonymous
  • 67
  • 7
  • From the error message it looks like you are trying to reach the database on `localhost` or `127.0.0.1`, when you should be connecting to `db:5432`. – super Feb 27 '22 at 10:22
  • @super sry for the confusion, do you mean i should change ```ports:- "5432:5432" ``` to ```ports:- "db:5432" ``` ? since as far as i understand, db is just a container name which is not a port address right? – Anonymous Feb 27 '22 at 10:27
  • No. `db` is the hostname of your container, available to all other containers in the same docker network. `docker-compose` adds all services to a default network is none is specified, so `sample` and `db` are in the same network already. In you source code where you try to connect to `db`, replace whatever ip you are using with `db`. – super Feb 27 '22 at 10:35
  • @super but my database is not remote, so i suppose the ip should be localhost or 127.0.0.1? – Anonymous Feb 27 '22 at 10:54
  • Do I really have to tell you a third time? Your containers all have different IP addresses. They are not considered the same machine as your host. If you want to connect from one container to another, they should be in the same docker network, and you use the service name as the hostname. – super Feb 27 '22 at 10:56
  • Btw, all this information is available in a ton of question here on SO if you just search for it. Also, any basic `docker-compose` tutorial will cover this. – super Feb 27 '22 at 10:59
  • It's also a little unusual for a React application to connect directly to a database. The networking there can be a little counterintuitive since a React application runs in the end user's browser, not the Docker container that's serving it. Is there a third back-end service container as part of this stack? – David Maze Feb 27 '22 at 11:58
  • @DavidMaze no, this is just a new project, and here is all we have so far for the backend part – Anonymous Feb 27 '22 at 12:01
  • As @super says, then, you don't use `localhost` to connect between containers: in Docker `localhost` almost always means "this container". Also see for example [ECONNREFUSED for Postgres on nodeJS with dockers](https://stackoverflow.com/questions/33357567/econnrefused-for-postgres-on-nodejs-with-dockers). – David Maze Feb 27 '22 at 12:10
  • @DavidMaze thank you for providing the reference, i've been searching solution on stakeoverflow for hours and hours but this is one of the most helpful one so far, which helped me a lot in understanding the problem! However, i think the question in the original post is not fully addressed. i see that his routing, i.e. ```DATABASE_URL``` was incorrect, however, he never used this env variable in docker compose as far as i can tell, so how does the solution make any difference then? – Anonymous Feb 27 '22 at 13:58
  • update: i think im closer to the solution now, connect ECONNREFUSED error longer exists, but a new issue getaddrinfo ENOTFOUND appears, which was also mentioned but not answered [here](https://stackoverflow.com/questions/33357567/econnrefused-for-postgres-on-nodejs-with-dockers) – Anonymous Feb 27 '22 at 15:08

0 Answers0