Connecting PostgreSQL db to Django project using Docker - django

I have a problem connecting my Postgres database to django project on docker container.
I have already tried every solution found on the internet and nothing seems to work.
I've already added host all all all md5 to my pg_hba.conf file, listen_addresses = '*' to postgresql.conf.
Here is log from lsof -i :5432:
postgres 19774 postgres 5u IPv4 2046377 0t0 TCP *:postgresql (LISTEN)
postgres 19774 postgres 6u IPv6 2046378 0t0 TCP *:postgresql (LISTEN)
I've tried to open 5432 port by using ufw allow 5432/tcp, it seems to be open.
My docker-compose.yml file:
version: '3.8'
services:
db:
image: postgres:12.0-alpine
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
build: .
container_name: db
volumes:
- postgres_data:/var/lib/postgresql/data/
networks:
- djangonetwork
ports:
- '5432'
web:
build:
context: .
dockerfile: Dockerfile
command: python manage.py runserver 127.0.0.1:8000
volumes:
- ./TwitterClone/:/usr/src/TwitterClone/
ports:
- 8000:8000
env_file:
- ./.env
depends_on:
- db
links:
- db:db
networks:
- djangonetwork
networks:
djangonetwork:
driver: bridge
volumes:
postgres_data:
settings.py:
try:
DATABASE_PASSWORD = os.environ.get('POSTGRES_PASSWORD')
DATABASE_USER = os.environ.get('POSTGRES_USER')
DATABASE_NAME = os.environ.get('POSTGRES_DB')
except:
print('Could not get environment variables')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DATABASE_NAME,
'USER': DATABASE_USER,
'PASSWORD': DATABASE_PASSWORD,
'HOST': '127.0.0.1',
'PORT': 5432,
}
}
No matter what I try to do, I always end up getting that error:
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Thank you in advance for any help, anything could help. Just let me know if you need more information from me, I will gladly provide it.
EDIT:
I have edited my docker-compose file by deleting all networks related lines. Now I don't get the previous error, but it seems to have a problem with my PostgreSQL server running on port 5432:
ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint db (a1ed9a70eddb931e5d2a26522cb95a2873e019029420f8b994146e6bebe5ce09): Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use

Related

Django, postgress docker-compose psycopg2.OperationalError

Set-up: postgres, django and redis services configured via docker-compose.
I've read through similar questions on SO but can't see why my compose config results in django not being able to see the db service. The db service is internally configured to port 5432 in the compose network, but mapped to 5433 on the host machine (5432 on the host is used by another docker db container). I'm not sure what I'm missing here, hoping someone can see something in the config or logs that I can't...
error logs from web service:
stage_web_0.10 | django.db.utils.OperationalError: could not connect to server: Connection refused
stage_web_0.10 | Is the server running on host "db" (172.25.0.2) and accepting
stage_web_0.10 | TCP/IP connections on port 5432?
Django settings from inside the container (correctly looking for db at 5432):
settings.DATABASES
{'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': 'db',
'PORT': '5432',
'NAME': 'stage',
'USER': 'admin',
'TEST': {'NAME': 'test_stage'}}}
docker-compose services
db:
container_name: stage_${DJANGO_ENV}_db_${TAG}
env_file:
- .env
build:
context:
./db/
args: # Make these available at build time to create the new db.
- POSTGRES_HOST
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_PORT
volumes:
- /data/postgresql/stage_${DJANGO_ENV}_${TAG}/:/var/lib/postgresql/data/
environment:
- TAG
- DJANGO_ENV
- POSTGRES_HOST
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_PORT
ports:
- "5433:5432"
expose:
- "5432" # Publishes 5432 to other containers but NOT to host machine
web:
container_name: stage_${DJANGO_ENV}_web_${TAG}
build:
context:
./web
dockerfile: Dockerfile.stage
args:
- PIP_INDEX_URL
- PIP_TRUSTED_HOST
env_file:
- .env
# Command gets appended to the entrypoint in the dockerfile and will be run
# when all containers are up (need postgres container started for migrations).
command: /code/start-django.sh
volumes:
- ./web/web_application/:/code/web
- ${STORAGE_PATH_HOST}:${STORAGE_PATH_CONTAINER}
# certificates
- /etc/apache2/ssl:/etc/apache2/ssl
ports:
- 8080:8000
depends_on:
- db
- redis
Logs for db container:
Attaching to stage_db_0.10
stage_db_0.10 | 2022-04-25 16:44:16.734 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
stage_db_0.10 | 2022-04-25 16:44:16.734 UTC [1] LOG: listening on IPv6 address "::", port 5432
stage_db_0.10 | 2022-04-25 16:44:16.782 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
stage_db_0.10 | 2022-04-25 16:44:16.867 UTC [19] LOG: database system was shut down at 2022-04-25 16:42:54 UTC
stage_db_0.10 | 2022-04-25 16:44:16.941 UTC [1] LOG: database system is ready to accept connections
docker ps shows correct mapping for db container
0.0.0.0:5433->5432/tcp, :::5433->5432/tcp stage_db_0.10
Any help much appreciated!

How to connect to local network database from docker container

I'm trying to connect to an express database on sql server accesible throughout 192.168.0.130:1433 on local network from Docker Django container.
I'm on a Mac and from local host i have ping
$ ping 192.168.0.130
64 bytes from 192.168.0.130: icmp_seq=0 ttl=128 time=5.796 ms
64 bytes from 192.168.0.130: icmp_seq=1 ttl=128 time=2.234 ms
But inside docker container get timeout error.
docker-compose.yml:
version: '3.7'
services:
...
django:
container_name: djangonoguero_django_ctnr
build:
context: .
dockerfile: Dockerfile-django
restart: unless-stopped
env_file: ./project/project/settings/.env
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./project:/djangonoguero
depends_on:
- postgres
ports:
- 8000:8000
networks:
- djangonoguero-ntwk
networks:
djangonoguero-ntwk:
driver: bridge
Anybody could help me please ?
Thanks in advance.
Reset fabric default values from Docker solved the problem.
For example, you have postgres container:
db:
image: postgres
environment:
# This is for example, not use this in production!!!
POSTGRES_USER: developer
POSTGRES_DB: db
POSTGRES_PASSWORD: Passw0rd33
...
Then, in your django project settings, you should write:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'db',
'USER': 'developer',
'PASSWORD': 'Passw0rd33',
'HOST': 'db',
}
}
Be careful, this is example settings! Best practice is to store this data in .env file!

failed to connect with Postgres container in Django

django.db.utils.OperationalError: connection to server at "db" (172.18.0.2), port 5432 failed: FATAL: the database system is starting up
I have an issue connecting to the Postgres contaner. I was trying in different ways like setting passwords only in the docker-compose file. Still am stuck.
docker-compose.yml
version: '3'
services:
db:
image: postgres:alpine
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=user
- POSTGRES_DB=userdb
volumes:
- django_db:/var/lib/postgresql/data
container_name: db
singup:
build: .
command: python manage.py runserver 0.0.0.0:8000
ports:
- 8000:8000
container_name: singup
depends_on:
- db
volumes:
django_db:
database setting
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'userdb',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'db',
}
}
This is a timing issue, even thought you have the below line
singup:
depends_on:
- db
It's just waiting for db container to be up and running, not neccessarily Postgres.
To avoid this, use a tool such as wait-for-it, dockerize, sh-compatible wait-for, or RelayAndContainers template. These are small wrapper scripts which you can include in your application’s image to poll a given host and port until it’s accepting TCP connections.
For example, to use wait-for-it.sh or wait-for to wrap your service’s command:
singup:
depends_on:
- db
command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
Repo of wait-for-it: https://github.com/vishnubob/wait-for-it

Pycharm Docker Debugger Unable to Connect to DB in Docker

I am running a PyCharm 2018.2 version.
I have my django code running on a docker instance and the Postgres running on another docker instance.
My docker-compose.yml file is here
version: "2.3"
services:
cv_db:
container_name: cv_db
image: postgres:10.3-alpine
restart: always
ports:
- "9081:5432"
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: ****
POSTGRES_DB: cv
volumes:
- cv_db:/var/lib/postgresql/data
expose:
- "9081:5432"
cv_redis:
container_name: cv_redis
image: redis:4.0.5-alpine
restart: always
ports:
- "6379:6379"
cv:
container_name: cv
image: cv
restart: always
depends_on:
- cv_redis
- cv_db
ports:
- "9080:9080"
- "8080:8080"
expose:
- "9080"
build:
context: .
args:
http_proxy:
https_proxy:
no_proxy:
TF_ANNOTATION: "no"
USER: "django"
DJANGO_CONFIGURATION: "production"
WITH_TESTS: "no"
environment:
DJANGO_MODWSGI_EXTRA_ARGS: ""
DJANGO_LOG_SERVER_URL: ""
volumes:
- cvat_data:/home/django/data
- cvat_keys:/home/django/keys
- cvat_logs:/home/django/logs
- ./share:/home/django/share
volumes:
cv_db:
cv_data:
cv_keys:
cv_logs:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
# 'HOST': 'cv_db',
'HOST': 'localhost',
'PORT': '9801',
'NAME': 'cv_dev',
'USER': 'root',
'PASSWORD': os.getenv('POSTGRES_PASSWORD', '****'),
}
I am using the django debugger:
Edit Configuration Settings are below:
Host: 0.0.0.0 Port: 9080
Run Browser: http://0.0.0.0:9080/
WHen I click on debug, I get the error saying it cant connect to Database:
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 9081?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 9081?
what might be going wrong?

docker compose cannot connect postgresql database with django

my django app cannot connect to postgresql. I'm using Dockerfile for django and build using docker-compose with postgres official image.
docker-compose.yml
version: '2'
services:
db:
image: eg_postgresql
expose:
- 5432
environment:
- POSTGRES_PASSWORD=docker
- POSTGRES_USER=docker
- POSTGRES_DB=postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/test_application
ports:
- "8000:8000"
links:
- "db:db"
environment:
- DATABASE_URL=postgres://docker:docker#db:5432/postgres
- DJANGO_SECRET_KEY=x7-g-xu^h5k%h8860!7ksn=#)7q9frn9_l6tmefvf)y=0)d!uh
output:
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 "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
docker-compose ps
ubuntu#ip-172-31-7-117:~/dj1/helloworld$ sudo docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------
helloworld_db_1 /usr/lib/postgresql/9.3/bi ... Up 5432/tcp
helloworld_web_1 python3 manage.py runserve ... Up 0.0.0.0:8000->8000/tcp
in app settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'docker',
'PASSWORD': 'docker',
'HOST': 'db',
'PORT': '5432',
}
}
I tried in many ways to connect but the issue is same...
Try replacing expose: - 5432 for your DB service with ports - "5432:5432"