【问题标题】:Django + Docker: connection to server at "localhost" (127.0.0.1), port 5432 failedDjango + Docker:连接到“localhost”(127.0.0.1)的服务器,端口 5432 失败
【发布时间】:2022-01-09 00:31:27
【问题描述】:

我正在尝试在 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 refused) 的答案,但没有解决我的问题

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

我正在尝试 sudo systemctl start postgresqlsudo systemctl enable postgresql(同样的错误)

【问题讨论】:

    标签: django postgresql docker nginx


    【解决方案1】:

    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 可以识别其他容器的解释可以在here 找到。

    【讨论】:

    • 我将 'localhost' 替换为 'db'。对于此更改,我有错误:django.db.utils.ProgrammingError:关系“django_session”不存在167.99.137.32/admin
    • 这意味着数据库连接现在可以工作了。但该错误与数据库中缺少表有关。你的 Dockerfile 看起来如何?您是否在启动时执行迁移?你会手动做吗?
    • 谢谢 python manage.py migrate 帮助了我。您能否将我链接到 django 如何理解“db”是正确的数据库 url 的详细信息?
    • 这是一个 Docker 的东西,Django 只是使用它。我使用Docker 文档链接编辑了我的回答
    猜你喜欢
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2019-08-15
    • 2020-01-21
    相关资源
    最近更新 更多