skip to Main Content

I have tried many possible solutions to it, Not able to make any of them work.
Here it goes:

I built a nodejs container and a postgres docker container. Used docker compose to configure them both and used Dockerfile to build the nodejs/typescript application.

docker-compose.yml

version: "3"

volumes: 
  pg_vol:

networks:
  app-network:
    driver: bridge

services:
  db:
    container_name: db
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=db22
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=q123
    volumes:
      - pg_vol:/var/lib/postgresql/data
    networks:
      - app-network
  webapp:
    container_name: webapp
    build:
      context: ./
      dockerfile: Dockerfile
    environment:
      - DATABASE_URL=postgres://postgres:q123@db:5432/db22
    ports:
      - "5000:5000"
    networks: 
      - app-network
    depends_on:
      - db

Dockerfile

FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY ./dist .
CMD [ "npm", "start" ]

When I do docker-compose up --build, it shows an error while connecting to the postgres db. DB starts alright.

Error STDOUT

Starting db ... done
Starting webapp ... done
Attaching to db, webapp
db        | 
db        | PostgreSQL Database directory appears to contain a database; Skipping initialization
db        | 
db        | 2020-11-24 16:32:25.918 UTC [1] LOG:  starting PostgreSQL 13.1 (Debian 13.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db        | 2020-11-24 16:32:25.918 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db        | 2020-11-24 16:32:25.918 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db        | 2020-11-24 16:32:25.923 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db        | 2020-11-24 16:32:25.928 UTC [25] LOG:  database system was shut down at 2020-11-24 16:32:18 UTC
db        | 2020-11-24 16:32:25.933 UTC [1] LOG:  database system is ready to accept connections
webapp    | 
webapp    | > [email protected] start /usr/src/app
webapp    | > node app.js
webapp    | 
webapp    | undefined
webapp    | undefined
webapp    | running in-code config
webapp    | Error: connect EHOSTUNREACH 172.19.0.2:5432
webapp    |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
webapp    |   errno: -113,
webapp    |   code: 'EHOSTUNREACH',
webapp    |   syscall: 'connect',
webapp    |   address: '172.19.0.2',
webapp    |   port: 5432
webapp    | }
webapp    | npm ERR! code ELIFECYCLE
webapp    | npm ERR! errno 1
webapp    | npm ERR! [email protected] start: `node app.js`
webapp    | npm ERR! Exit status 1
webapp    | npm ERR! 
webapp    | npm ERR! Failed at the [email protected] start script.
webapp    | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
webapp    | 
webapp    | npm ERR! A complete log of this run can be found in:
webapp    | npm ERR!     /root/.npm/_logs/2020-11-24T16_32_26_955Z-debug.log
webapp exited with code 1

Please help with correcting me.

System:

Fedora 33 (Workstation Edition)

EDIT:
I restarted the docker container with command docker start webapp. Same error as above.

2

Answers


  1. Chosen as BEST ANSWER

    My system is Fedora 33 (Workstation Edition). This had some issues with Docker.

    Turns out: With the release of Fedora 33, Docker is not supported by Fedora 33 officially. Oct 29, 2020

    I uninstalled docker. Rebooted my laptop. Then install Podman and Podman Compose. Everything works perfectly fine.

    Thank you all for help.


  2. Judging by logs: the app exits with 1 because the host at port 5432 was unreachable,maybe the database was not ready yet (you can verify it by checking if db22 is created on db container ).What you can is : add restart policy on webapp ,using this :
    webap : ..
    restart_policy: condition: on-failure
    then check then if the problem persists

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