0

I am currently having some problems with docker and connecting a node project with postgres when running inside it. My docker-compose file looks like:

# docker-compose.yml
version: "3"

services:
  app:
    build: .
    ports:
      - "49160:8080"
    links:
      - db
    depends_on:
      - db

  db:
    image: kartoza/postgis:9.6-2.4
    environment:
      - POSTGRES_DB=mydb
      - POSTGRES_USER=postgres
      - POSTGRES_PASS=mypassword
      - POSTGRES_MULTIPLE_EXTENSIONS=postgis,pgrouting
      - ALLOW_IP_RANGE=0.0.0.0/0
    volumes:
      - pgdata:/var/lib/postgresql/data

    restart: always

volumes:
  pgdata: {}

my Dockerfile looks as following:

#node 8
FROM node:8

#Create app directory
WORKDIR /usr/src/app

#Install app dependencies
COPY package*.json ./

RUN npm install

#Bundle app source
COPY . . 

EXPOSE 8080

CMD ["npm", "start"]

my express server has following settings:

const { Client } = require('pg');

const client = new Client({
    user: 'postgres',
    host: 'db',
    database: 'mydb',
    password: 'mypassword',
    port: 5432
})

client.connect()

When I run docker-compose up, I first get password authentification error:

postgres@mydb FATAL:  password authentication failed for user "postgres"
postgres@mydb DETAIL:  User "postgres" has no password assigned.

In order to fix this errors I did following steps:

1a) Opened pg_hba.conf in postgres container and changed the line

local all postgres peer

to

local all postgres trust

1b) Open postgresql.conf and changed the line

listen_addresses = 'localhost' to listen_addresses = '*'

2) Restarted the server

service postgresql restart

3) Login into psql and set password

ALTER USER postgres with password 'mypassword';

4) Then I restored my sql backup file into my db within the container

But when I run my node app in my nodejs container

sudo docker exec node_container_id node index.js

I got following error:

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE 0.0.0.0:8080
    at Server.setupListenHandle [as _listen2] (net.js:1360:14)
    at listenInCluster (net.js:1401:12)
    at doListen (net.js:1510:7)
    at _combinedTickCallback (internal/process/next_tick.js:142:11)
    at process._tickCallback (internal/process/next_tick.js:181:9)
    at Function.Module.runMain (module.js:696:11)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3

I have no other process/program, that runs on port 8080, When I do killall node it throws me out from my docker container. I even changed the port from 8080 to 3000 but still the same error.

Candy
  • 95
  • 1
  • 10
  • If you need to restart the process running in a Docker container, just delete and recreate the container. (Your `docker exec` is trying to run a second instance of the same process in the same container, which leads to your error.) – David Maze Jul 03 '19 at 16:41
  • You should stop you container and restart it, when you use `docker exec` you will start a new container with the same port `8080`. If you had apply the modification inside the container you should commit the modification `docker commit` to not lose it. **try to debug why the password on docker container env not set correctly** – seddikomar Jul 03 '19 at 18:01
  • what do you mean with 'why the password on docker container env not set correcltly'? How can I debug my code from the docker nodeJS container? – Candy Jul 03 '19 at 19:46
  • @David Maze. How do I run my index.js (which is in the docker conatiner) then ? I want to do something like `node index.js` or `npm run start` ...so that I can see any error messages, which occur when I fire a get request. – Candy Jul 03 '19 at 19:49
  • The problem with which I struggle is, that when I run my app at localhost:49160 it doesn't output the exepecting result from the postgresDB, I can't even see an error message... – Candy Jul 03 '19 at 20:01
  • Yes, your Dockerfile says the default command when you run the image is `npm start`, so starting the container runs that. If you want to see what the container prints out, don't use the `-d` option to `docker-compose up` or `docker run`, or use `docker-compose logs` or `docker logs` to look after the fact. – David Maze Jul 03 '19 at 20:21
  • ok, got it! Thanks :) but I am getting another error message saying that `Error: connect ECONNREFUSED 172.24.0.2:5432` – Candy Jul 03 '19 at 20:25

0 Answers0