I have everything on one network (it talks to another app in another docker-compose file).
But I keep getting this error:
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "db" (192.168.208.3) and accepting
TCP/IP connections on port 5000?
When I change my SQL_PORT=5432
(default port that's running in the postgres container) the error above disappears and my app is up, but then it has problems when trying to connect to celery or in shell it says db is not connected.
I have to use 5000 cause there is another postgres db on the other app in the second docker-compose setup.
I think I'm lost on the internals of networks. I was pretty sure I am suppose to use the exposed port of 5000 for my database.
version: "3.9"
services:
app:
build: .
command: python manage.py runserver 0.0.0.0:8000
container_name: app
environment:
- DEBUG=True
- PYTHONUNBUFFERED=1
- CELERY_BROKER=redis://broker:6379/0
- CELERY_BACKEND=redis://broker:6379/
- APP_BASIC_AUTH_PASSWORD=adPswd12*
- APP_BASIC_AUTH_USER=admin
- APP_TOKEN_AUTH=NHEC_UTILITY
- VTN_API_URL=vtn_app:8000
- VTN_API_TOKEN=NHECAPP_UTILITY
- SQL_PORT=5000
volumes:
- .:/code
ports:
- "9000:8000"
networks:
- app-web-net
depends_on:
- db
- celery-worker
- broker
app_test:
build: .
command: python manage.py test
container_name: app_test
environment:
- DEBUG=True
- PYTHONUNBUFFERED=1
volumes:
- .:/code
depends_on:
- db
db:
image: postgres:10
container_name: app_postgres
ports:
- 5000:5432
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_DB: nhec
POSTGRES_USER: nhec
POSTGRES_PASSWORD: nhec
networks:
- app-web-net
celery-worker:
build: .
command: celery -A app worker -l DEBUG
depends_on:
- db
- broker
environment:
CELERY_BROKER_URL: redis://broker:6379/0
networks:
- app-web-net
broker:
image: redis:6-alpine
ports:
- 6379:6379
networks:
- app-web-net
volumes:
db_data: {}
networks:
app-web-net:
driver: bridge
The ports
will expose the postgres server on the your localhost
port 5000, not to internal services. Your services will be able to reach the database container and its ports within the same network.
If you still want to change the default postgres port, here's a related answer that you might find helpful. Changing a postgres containers server port in Docker Compose
Your container name is actually app_postgres
, not db
as specified by this in your docker compose:
container_name: app_postgres
The other thing you can do is change the name of the container from app_postgres
to something not the same as the postgres in your other app's docker compose file. Both postgres instances can have port 5432 exposed for your apps to use.