我正在嘗試在 docker 中運行我的 Django 應用程式(Nginx、Gunicorn)。
但是對于請求http://167.99.137.32/admin/我有錯誤:(完整日志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?
我正在嘗試Can't run the server on Django (connection denied) 的答案,但沒有解決我的問題
設定.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lk_potok_2',
'USER': 'postgres',
'PASSWORD': 'post222',
'HOST': 'localhost',
'PORT': 5432,
},
碼頭工人-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/;
}
}
我正在嘗試sudo systemctl start postgresql和sudo systemctl enable postgresql(同樣的錯誤)
uj5u.com熱心網友回復:
postgres 資料庫不再運行在localhost. 在您的情況下(因為您命名了 container db)它是db.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lk_potok_2',
'USER': 'postgres',
'PASSWORD': 'post222',
'HOST': 'db',
'PORT': 5432,
},
我真的不明白你為什么要在這里添加這個:
environment:
- DATABASE_URL=postgres://postgres:post222@localhost:5432/lk_potok_2"
因為你不在你的settings.py. 但在這里它也必須db代替localhost.
- 編輯 -
docker可以在這里找到為什么可以識別其他容器的解釋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/406262.html
標籤:
上一篇:Django限制圖片上傳大小
