I have a Docker compose YML file that has 2 containers defined in it, and when I run my Docker compose, I get a "ECONNREFUSED 0.0.0.0:5432" error.
Looking at the error logs, the database server (Postgres) container is running & ready for incoming connections, but when the application (Node js app) container
The weird thing is that they're on the same network, and on both containers the "0.0.0.0:5432" port has been set & exposed, but they're still not connecting when the application container makes a database call (see below image)

Docker compose:
version: "3.3"
services:
# container 1
application:
ports:
- "8000:8000"
build:
context: .
dockerfile: ./dockerfile-app
volumes:
- .:/docker-app
depends_on:
database:
condition: service_healthy
restart:
always
networks:
- one_network
# container 2
database:
image:
postgres:12.6
ports:
- "5432:5432"
volumes:
- ./postgres-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
environment:
POSTGRES_DB: "postgres"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
restart:
always
networks:
- one_network
networks:
one_network:
Dockerfile:
# Base image
FROM ubuntu:16.04
# System updates
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt -y install -y software-properties-common curl
# Install Nodejs
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash - && apt install -y nodejs
# Copy bash file for post-build tasks
WORKDIR /docker-app
COPY /docker-app.sh .
# Install Node packages
COPY /package.json .
RUN echo $PWD
RUN npm install
# Runs post-build commands
CMD ["sh", "./docker-app.sh"]
Can anyone help me understand why I'm getting this error please?
So I managed to solve this issue... which wasn't anything to do with Docker, but rather in my Node js app config:
module.exports = {
client: 'pg',
connection: {
// host is the same name as the Docker container DB service
host: 'database',
port: '5432',
user: 'postgres',
password: 'postgres',
database: 'postgres'
},
Note: the 'host' variable, it's not localhost or 127.0.0.1 or anything similar. Rather it is the NAME of the container running the database (named above in my Docker Compose file)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With