docker compose cannot connect postgresql database with django - 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"

Related

Django Is the server running on host "localhost" (127.0.0.1) and accepting web | TCP/IP connections on port 5432? [duplicate]

This question already has an answer here:
Django Is the server running on host "localhost" (127.0.0.1) and accepting web-1 | TCP/IP connections on port 5432? [duplicate]
(1 answer)
Closed 5 months ago.
I am trying to dockerize my Django application. My configs are the following:
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost',
'PORT': '5432',
}
}
.env file
POSTGRES_USER='DB_USER'
POSTGRES_PASSWORD='DB_PASSWORD'
POSTGRES_DB='DB_NAME'
POSTGRES_HOST='localhost'
POSTGRES_PORT='5432'
Dockerfile.web
FROM python:3.9-bullseye
WORKDIR /app
ENV PYTHONUNBUFFERED=1
COPY csgo .
RUN apt-get update -y \
&& apt-get upgrade -y pip \
&& pip install --upgrade pip \
&& pip install -r requirements.txt
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
docker-compose.yml
version: '3'
services:
web:
container_name: web
build:
context: .
dockerfile: Dockerfile.web
env_file:
- .env
volumes:
- .:/code
ports:
- '8000:8000'
depends_on:
- db
db:
image: postgres:13
restart: always
ports:
- '5432:5432'
env_file:
- .env
volumes:
postgres_data:
Now, when I docker-compose up all appear to be working fine besides connection to the database. I get this error:
django.db.utils.OperationalError: could not connect to server: Connection refused
web | Is the server running on host "localhost" (127.0.0.1) and accepting
web | TCP/IP connections on port 5432?
web | could not connect to server: Cannot assign requested address
web | Is the server running on host "localhost" (::1) and accepting
web | TCP/IP connections on port 5432?
Can you please help me solve this problem? Additionally, I would be grateful for broader explanation about the localhost definition in docker containers and its connections.
In a Docker context, localhost means the container itself.
Docker compose sets up a docker bridge network and connects the containers to it. From one container, you can connect to another container using it's service name.
So you need to use db as the hostname for the database, like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'db',
'PORT': '5432',
}
}
As far as I can see, you don't use the environment variables, but you might need to change it there as well.

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

Connecting PostgreSQL db to Django project using Docker

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

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?

Running Django migrations on dockerized project

I have a Django project running in multiple Docker containers with help of docker-compose. The source code is attached from directory on my local machine. Here's the compose configuration file:
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432:5432'
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8001:8000'
volumes:
- .:/code
depends_on:
- db
Although the application starts as it should, I can't run migrations, because every time I do manage.py makemigrations ... I receive:
django.db.utils.OperationalError: could not translate host name "db" to address: nodename nor servname provided, or not known
Obviously I can open bash inside the core container and run makemigrations from there, but then the migration files are created inside the container which is very uncomfortable.
In my project's settings the database is configured as:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': '5432',
}
}
As docker postgres image is accessible at localhost:5432 I tried changing database host in settings to:
'HOST': '0.0.0.0'
But then when firing up the containers with docker-compose up I'm receiving:
...
django.db.utils.OperationalError: could not connect to server:
Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
...
How should I configure database in settings.py so that Django can access it to create migrations?
Your docker-compose configurations are not correct. You forgot to link services
version: '3'
services:
db:
image: 'postgres'
ports:
- '5432:5432'
core:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- '8001:8000'
volumes:
- .:/code
depends_on:
- db
links: # <- here
- db