Reputation: 732
I am trying to run my Django app (Nginx, Gunicorn) in docker.
But for request http://167.99.137.32/admin/ I have error: (full log https://pastebin.com/0f8CqCQM)
onnection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Address not available
Is the server running on that host and accepting TCP/IP connections?
I was trying answers from Can't run the server on Django (connection refused) but didn't solve my problem
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lk_potok_2',
'USER': 'postgres',
'PASSWORD': 'post222',
'HOST': 'localhost',
'PORT': 5432,
},
docker-compose.yml
version: '3.9'
services:
django:
build: . # path to Dockerfile
command: sh -c "gunicorn --bind 0.0.0.0:8000 potok.wsgi:application"
volumes:
- .:/project
- static:/project/static
expose:
- 8000
environment:
- DATABASE_URL=postgres://postgres:post222@localhost:5432/lk_potok_2"
- DEBUG=1
db:
image: postgres:13-alpine
volumes:
- pg_data:/var/lib/postgresql/data/
expose:
- 5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=post222
- POSTGRES_DB=lk_potok_2
nginx:
image: nginx:1.19.8-alpine
depends_on:
- django
ports:
- "80:80"
volumes:
- static:/var/www/html/static
- ./nginx-conf.d/:/etc/nginx/conf.d
volumes:
pg_data:
static:
nginx-conf.nginx
upstream app {
server django:8000;
}
server {
listen 80;
server_name 167.99.137.32;
location / {
proxy_pass http://django:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /var/www/html/static/;
}
}
I was trying sudo systemctl start postgresql and sudo systemctl enable postgresql (the same error)
Upvotes: 16
Views: 38758
Reputation: 13
The order in which docker executes building the containers matters. I ran into the same issue because I was trying to execute my "web" container before executing the "db" container, so it kept failing to connect to the database because it hadn't initialized yet.
Swapping the container build order in my docker-compose.yml
file worked for me and eliminated the Is the server running on that host and accepting TCP/IP connections?
error.
Correct order below:
services:
db:
image: postgres:17
container_name: db
ports:
- "5433:5432"
environment:
POSTGRES_USER: username
POSTGRES_PASSWORD: password
POSTGRES_DB: my_application
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/my_application
ports:
- "8000:8000"
Upvotes: 0
Reputation: 1
I had a same problem: I tried host.docker.internal
, localhost
, 127.0.0.1
not working.
I finally found it. It was 0.0.0.0
for me.
Upvotes: 0
Reputation: 157
I was using podman on windows. Restarting the podman machine worked.
podman machine stop
podman machine start
Upvotes: 0
Reputation: 3491
I had this issue when i forget to add -p 5432:5432
Check that you explicitly specify the port that will be used by the container.
docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=p4SSW0rd postgres
Upvotes: 0
Reputation: 556
If you are using Linux environment make sure to set the host with the name of your database service, suppose my database service is like this:
db:
image: postgres:13.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./config/dev.env.db
ports:
- "5432:5432"
On my environment host will be db
if you are using windows/mac make sure you are using for the
host : host.docker.internal
you should also map the database server port in your dockercompose.yml file by default 5432:5432
Upvotes: 2
Reputation: 311
I did the following
Upvotes: 0
Reputation: 857
The postgres database is no longer running at localhost
. In your case (since you named the container db
) it is db
.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lk_potok_2',
'USER': 'postgres',
'PASSWORD': 'post222',
'HOST': 'db',
'PORT': 5432,
},
I don't really see why you would add this in here:
environment:
- DATABASE_URL=postgres://postgres:post222@localhost:5432/lk_potok_2"
since you don't use it in your settings.py
. But here it wil also have to be db
instead of localhost
.
--EDIT--
Explanation as why docker
can recognise the other containers can be found here.
Upvotes: 28