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)
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.