简体   繁体   English

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

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

I have a Django app that runs locally with no problems.我有一个 Django 应用程序在本地运行没有问题。 Now, I want to get a docker image of my app but when I try to build it, it gives me the following error:现在,我想获得我的应用程序的 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?

I am new on Django and Docker development and i was looking for similar questions but none answer solved my problem.我是 Django 和 Docker 开发的新手,我一直在寻找类似的问题,但没有答案能解决我的问题。

I'll show you my Dockerfile, I have copied the Postgres commands from another project that does work:我将向您展示我的 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"]

Here is my requirements.txt file:这是我的 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

Here is my settings.py file (the databases fragment):这是我的 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'),
    }
}

And here is my.env file:这是 my.env 文件:

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

Some people told me that I could make a docker-compose file so I erased the postgres install commands from Dockerfile and made this:有人告诉我,我可以制作一个 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

But this doesn't work either.但这也不起作用。

Footnote: If I use the Django's default database (SQLite) and (obviously) erase postgress commands from Dockerfile, I can build the Docker image without problems, and, again, this app with postgres works good if I run it locally.脚注:如果我使用 Django 的默认数据库 (SQLite) 并且(显然)从 Dockerfile 中删除 postgress 命令,我可以毫无问题地构建 Docker 图像,而且,如果我在本地运行这个带有 postgres 的应用程序,它再次运行良好。 So, something is happening with Docker + postgres and I don't know what to do.所以,Docker + postgres 发生了一些事情,我不知道该怎么办。

Somebody can help me?有人可以帮助我吗? Thank you!谢谢!

While creating a superuser your command is创建superuser时,您的命令是

CREATE USER admin WITH SUPERUSER PASSWORD 'administrador'

And in your env file you are using passwd as password.在您的env文件中,您使用passwd作为密码。

Change your env from改变你的env

POSTGRESQL_PASS=passwd

To this对此

POSTGRESQL_PASS=administrador

暂无
暂无

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

相关问题 Docker- django 在连接到 postgres 时抛出错误:psycopg2.OperationalError:无法连接到服务器:连接被拒绝 - Docker- django throws error while connecting to postgres: psycopg2.OperationalError: could not connect to server: Connection refused Docker Django 无法连接到服务器:连接被拒绝 - Docker Django could not connect to server: Connection refused (psycopg2.OperationalError)无法连接到服务器:连接被拒绝是服务器 - (psycopg2.OperationalError) could not connect to server: Connection refused Is the server 得到 django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - Got django.db.utils.OperationalError: could not connect to server: Connection refused django postgres无法连接到服务器:连接被拒绝服务器在主机“ localhost”上运行吗(127.0.0.1) - django postgres could not connect to server: Connection refused Is the server running on host “localhost” (127.0.0.1) Django-OperationalError:无法连接到服务器 - Django - OperationalError: could not connect to server django postgres 无法连接到服务器 - django postgres could not connect to server Flask-SQLAlchemy Postgres 错误 - 无法连接到服务器:连接被拒绝 (0x0000274D/10061) - Flask-SQLAlchemy Postgres Error - could not connect to server: Connection refused (0x0000274D/10061) 流浪者上的Django,psycopg2 OperationalError:无法连接到服务器:没有这样的文件或目录 - Django on vagrant, psycopg2 OperationalError: could not connect to server: No such file or directory django.db.utils.OperationalError:无法连接到服务器:没有这样的文件或目录 - django.db.utils.OperationalError: could not connect to server: No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM