繁体   English   中英

Docker/Django 应用程序中的 Postgres 不起作用 - OperationalError:无法连接到服务器:连接被拒绝

[英]Postgres in Docker/ Django app does not work - OperationalError: could not connect to server: Connection refused

我有一个 Django 应用程序在本地运行没有问题。 现在,我想获得我的应用程序的 docker 映像,但是当我尝试构建它时,它给了我以下错误:

django.db.utils.OperationalError: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?

我是 Django 和 Docker 开发的新手,我一直在寻找类似的问题,但没有答案能解决我的问题。

我将向您展示我的 Dockerfile,我已经从另一个有效的项目中复制了 Postgres 命令:

FROM python:3.8

RUN apt-get update

RUN mkdir /project
WORKDIR /project

RUN apt-get install -y vim
COPY requirements.txt /project/
RUN pip install -r requirements.txt
COPY . /project/

# Install postgresql
RUN apt install -y postgresql postgresql-contrib
RUN service postgresql start

# Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
USER postgres

# Create a PostgreSQL role
RUN /etc/init.d/postgresql start &&\
    psql --command "CREATE USER admin WITH SUPERUSER PASSWORD 'passwd';" &&\
    createdb -O admin plataforma

USER root

# setup postgresql
RUN sed -i "/^#listen_addresses/i listen_addresses='*'" /etc/postgresql/13/main/postgresql.conf
RUN sed -i "/^# DO NOT DISABLE\!/i # Allow access from any IP address" /etc/postgresql/13/main/pg_hba.conf
RUN sed -i "/^# DO NOT DISABLE\!/i host all all 0.0.0.0/0 md5\n\n\n" /etc/postgresql/13/main/pg_hba.conf


# running commands of my app
RUN python manage.py makemigrations accounts
RUN python manage.py sqlmigrate accounts 0001
RUN python manage.py migrate
RUN python manage.py inicializar

# Expose some ports
EXPOSE 22 5432 8080 8009 8000

# volumes
VOLUME ["/var/lib/postgresql/12/main"]


# Default command
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

这是我的 requirements.txt 文件:

#requirements.txt
Django==3.2.8
djangorestframework==3.9.1
gunicorn==19.9.0
pandas==1.3.3
path==16.2.0
six==1.14.0
Pillow==7.0.0
psycopg2>=2.8
django-environ==0.8.1

这是我的 settings.py 文件(数据库片段):


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': env('POSTGRESQL_NAME'),
        'USER': env('POSTGRESQL_USER'),
        'PASSWORD': env('POSTGRESQL_PASS'),
        'HOST': env('POSTGRESQL_HOST'),
        'PORT': env('POSTGRESQL_PORT'),
    }
}

这是 my.env 文件:

POSTGRESQL_NAME=plataforma
POSTGRESQL_USER=admin
POSTGRESQL_PASS=passwd
POSTGRESQL_HOST=localhost
POSTGRESQL_PORT=5432
DEBUG=True

有人告诉我,我可以制作一个 docker-compose 文件,所以我从 Dockerfile 中删除了 postgres 安装命令并做了这个:

version: "3.3"

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    ports:
      - "5432:5432"
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=plataforma
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=passwd
    depends_on:
      - db

但这也不起作用。

脚注:如果我使用 Django 的默认数据库 (SQLite) 并且(显然)从 Dockerfile 中删除 postgress 命令,我可以毫无问题地构建 Docker 图像,而且,如果我在本地运行这个带有 postgres 的应用程序,它再次运行良好。 所以,Docker + postgres 发生了一些事情,我不知道该怎么办。

有人可以帮助我吗? 谢谢!

创建superuser时,您的命令是

CREATE USER admin WITH SUPERUSER PASSWORD 'administrador'

在您的env文件中,您使用passwd作为密码。

改变你的env

POSTGRESQL_PASS=passwd

对此

POSTGRESQL_PASS=administrador

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM