簡體   English   中英

Django + Docker:連接到“localhost”(127.0.0.1)的服務器,端口 5432 失敗

[英]Django + Docker: connection to server at "localhost" (127.0.0.1), port 5432 failed

我正在嘗試在 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,
    },

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/;
    }
}

我正在嘗試sudo systemctl start postgresqlsudo systemctl enable postgresql (同樣的錯誤)

postgres 數據庫不再在localhost運行。 在您的情況下(因為您將容器命名為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可以識別其他容器的解釋可以在這里找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 Postgresql 中的錯誤:連接到“localhost”(127.0.0.1)的服務器,端口 5432 失敗:致命:用戶“TheGecko”的密碼驗證失敗 Django 在 App Engine 上部署引發錯誤:連接到“127.0.0.1”的服務器,端口 5432 失敗: EC2 和 RDS 上的 Django 應用程序(服務器是否在主機“localhost”(127.0.0.1)上運行並在端口 5432 上接受 TCP/IP 連接?) 連接到“localhost”的服務器,端口 5432 失敗:連接被拒絕服務器是否在該主機上運行並接受 TCP/IP 連接? 無法連接到服務器:連接被拒絕。 服務器是否在主機“localhost”(127.0.0.1) 上運行並在端口 5432 上接受 TCP/IP 連接 服務器是否在主機“ localhost”(127.0.0.1)上運行並在端口5432上接受TCP / IP連接? 505錯誤無法連接到服務器:連接被拒絕服務器是否在主機“ 127.0.0.1”上運行並接受端口5432上的TCP / IP連接? Cloud Foundry Django 應用程序“cf push”記錄錯誤“端口 5432 失敗:連接被拒絕” Django, Docker, postgreSQL 在端口 5432 上接受 TCP/IP 連接? django postgres無法連接到服務器:連接被拒絕服務器在主機“ localhost”上運行嗎(127.0.0.1)
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM