I’m trying to deploy a Golang app using docker compose but unfortunately the PostgreSQL DB connection isn’t working.
Here’s my docker-compose.yml
file:
services:
db:
image: postgres:16.3-alpine
container_name: "backend-db"
ports:
- "5432:5432"
restart: always
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USERNAME}
- POSTGRES_PASSWORD=${DB_PASSWORD}
networks:
- fullstack
volumes:
- database_postgres:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME}"]
interval: 3s
timeout: 3s
retries: 5
expose:
- "5432"
api:
build: .
container_name: "backend-api"
ports:
- "4000:4000"
depends_on:
db:
condition: service_healthy
networks:
- fullstack
volumes:
database_postgres:
migrations:
networks:
fullstack:
driver: bridge
I’m doing a docker compose up --build
to test the app locally and whenever my app tries to interact with the DB, I get:
dial tcp [::1]:5432: connect: connection refused
However, if I change in the api
networks: fullstack
by - network_mode: "host"
I get pq: SSL is not enabled on the server
.
Please notice that I’m using an .env
file to load the environment variables, and in my case, the variable REMOTI_DB_DSN
has appended sslmode=disable
but it looks like it’s not being picked up.
Thanks a lot in advance!