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.