[英]Docker- django throws error while connecting to postgres: psycopg2.OperationalError: could not connect to server: Connection refused
[英]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.