[英]Django: OperationalError when connecting to PostgreSQL DB on Docker
I have a problem with connecting Django with PostgreSQL installed in docker, when run Django using当使用运行 Django 时,我在将 Django 与安装在 docker 中的 PostgreSQL 连接时遇到问题
python manage.py runserver
it returns the following error它返回以下错误
OperationalError: could not translate host name "db" to address: Unknown host
settings.py:设置.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'trytofindme',
'HOST': 'db',
'PORT': '5432',
}
}
Dockerfile: Dockerfile:
# pull official base image
FROM python:3.6.4-alpine
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install wkhtmltopdf dependencies
RUN wget https://s3.amazonaws.com/shopify-managemant-app/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
RUN tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
RUN mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
RUN chmod +x /usr/local/bin/wkhtmltopdf
# install python dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN apk --update add libxml2-dev libxslt-dev libffi-dev gcc musl-dev libgcc openssl-dev curl
RUN apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev
RUN \
apk add --no-cache postgresql-libs && \
apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev && \
python3 -m pip install -r requirements.txt --no-cache-dir && \
apk --purge del .build-deps
# copy project
COPY . /usr/src/app/
docker-compose:码头工人组成:
version: '3.7'
services:
db:
image: postgres:13.0
restart: always
environment:
POSTGRES_PASSWORD: trytofindme
ports:
- 15432:5432
adminer:
image: adminer
restart: always
ports:
- 8020:8080
I can't find mistake in my code.我在我的代码中找不到错误。 Are there any variant to connect PostgreSQL and Django, not loosing the protection?
是否有任何变体可以连接 PostgreSQL 和 Django 而不会失去保护?
I used:我用了:
docker-compose up -d --build
And then:进而:
docker-compose up
Docker container starts working and said, that db was ready to accept the connections. Docker 容器开始工作并说 db 已准备好接受连接。 So I think, that problem is in my Django setup
所以我认为,这个问题出在我的 Django 设置中
The host has to be a hostname, so an address to your database.主机必须是主机名,因此是数据库的地址。 In your case, it should be set to either
localhost
or 127.0.0.1
.在您的情况下,它应该设置为
localhost
或127.0.0.1
。 Django has no knowledge of the logical names of your Docker services. Django 不知道您的 Docker 服务的逻辑名称。
Your docker-compose.yaml
also specifies a different host port than the ones you put in your Django config (see docs ):您
docker-compose.yaml
还指定了与您在 Django 配置中放置的主机端口不同的主机端口(请参阅文档):
Either specify both ports (HOST:CONTAINER), or just the container port (an ephemeral host port is chosen).
指定两个端口 (HOST:CONTAINER),或仅指定容器端口(选择临时主机端口)。
You should either change the port in your Django config to 15432
, or better change the port mapping as follows您应该将 Django 配置中的端口更改为
15432
,或者更好地更改端口映射如下
...
services:
db:
...
ports:
- "5432:5432"
...
Using a standard port like 5432
is preferable because it makes it easier to identify host services/processes from the open ports on the host.最好使用像
5432
这样的标准端口,因为它可以更容易地从主机上的开放端口识别主机服务/进程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.