Django + Docker:连接到服务器“localhost”(127.0.0.1),端口5432失败。

10

我正在尝试在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?

我尝试了无法在Django上运行服务器(连接被拒绝)的答案,但没有解决我的问题。

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(出现了相同的错误)

4个回答

18

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中没有使用它,因此此处也必须将localhost更改为db

--编辑--

关于为什么docker可以识别其他容器的解释可以在这里找到。


我将“localhost”替换为“db”。由于这个更改,我遇到了错误:django.db.utils.ProgrammingError: relation "django_session" does not exist。http://167.99.137.32/admin/ - Alex
1
这意味着数据库连接现在正常工作。但是该错误与您的数据库中缺少表有关。您的Dockerfile是什么样子的?您是否在启动时执行迁移?还是手动执行它们? - DrummerMann
谢谢python manage.py migrate帮了我。你能否给我提供链接,详细介绍Django如何理解'db'是正确的数据库URL? - Alex
1
这是一个Docker的事情,Django只是使用它。我编辑了我的答案,并附上了Docker文档链接。 - DrummerMann
使用Docker Compose时,我解决了连接PostgreSQL数据库的问题,该数据库是与Web服务一起使用的。 - orb

2

如果您正在使用Linux环境,请确保将主机设置为您的数据库服务名称,假设我的数据库服务如下:

 db:
      image: postgres:13.0-alpine
      volumes:
       - postgres_data:/var/lib/postgresql/data/
      env_file:
       - ./config/dev.env.db
      
      ports:
          - "5432:5432"

在我的环境中,主机将是db。如果您使用的是Windows/Mac,请确保您正在使用该主机。
host : host.docker.internal

你还应该在dockercompose.yml文件中默认映射数据库服务器端口5432:5432


0

当我忘记添加-p 5432:5432时,我遇到了这个问题。

请检查您是否明确指定了容器将使用的端口。

docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=p4SSW0rd postgres

0

我做了以下操作

  1. 我们获取占用端口的应用程序: lsof -i tcp:5432
  2. 使用Kill命令,终止占用端口5432的进程

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接