I have postgresSQL database running docker on server when i spin up another container for django app and trying to connect postgress getting connection error. any idea?
django.db.utils.OperationalError: connection to server at "localhost" (127.0.0.1), port 6545 failed: Connection refused Is the server running on that host and accepting TCP/IP connections? connection to server at "localhost" (::1), port 6545 failed: Cannot assign requested address Is the server running on that host and accepting TCP/IP connections?
DB docker file
container_name: pg-docker
ports:
- "6545:5432"
volumes:
- ./data:/var/lib/postgresql/data
networks:
- default
Django docker file
version: "3.9"
services:
django_api:
build:
context: ./app
dockerfile: Dockerfile
container_name: api-dev
command: python manage.py runserver 0.0.0.0:8000
ports:
- 8000:8000
networks:
- default
CodePudding user response:
Instead of this:
ports:
- "6545:5432"
Try this way:
ports:
- "5432:6545"
OR
you can also try this way:
ports:
- "6545:6545"
expose:
- "6545"
CodePudding user response:
As @JustLudo said in the Comments, you have to address postgres with the container name "pg-docker". Localhost would be your django container. In general, if you use multiple docker containers you should not use localhost. Instead treat every container as a standalone server and address via DNS / container_name.