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)
4
Answers
The postgres database is no longer running at
localhost
. In your case (since you named the containerdb
) it isdb
.I don’t really see why you would add this in here:
since you don’t use it in your
settings.py
. But here it wil also have to bedb
instead oflocalhost
.–EDIT–
Explanation as why
docker
can recognise the other containers can be found here.I did the following
lsof -i tcp:5432
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:
On my environment host will be
db
if you are using windows/mac make sure you are using for the
you should also map the database server port in your dockercompose.yml file by default 5432:5432
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.