Py学习  »  Django

将Django docker容器链接到postgres容器

Manuel Santi • 4 年前 • 565 次点击  

我创建了一个django docker应用程序映像。 在我的django应用程序中,在settings.py数据库中的条目是:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'cath_local',
    'USER': 'postgres',
    'PASSWORD': 'mypass',
    'HOST': 'postgres',
    'PORT': '5432',
    'OPTIONS': {
        'client_encoding': 'UTF8',
    },
}
}

在这一点上,我用 docker build 命令;全部完成。 在运行我的django应用程序docker映像之前,我运行:

docker run -it postgres

已下载图像并正确启动容器

enter image description here

但是当我运行我的django应用程序时

docker run -it cath2019/cathedral_studio:latest

连接=连接(dsn,连接工厂=连接工厂,**kwasync) django.db.utils.operational错误:无法将主机名“postgres”转换为地址:名称无法解析

这里是我的应用程序摘要文件:

FROM python:3.6-alpine
RUN apk add --no-cache make linux-headers libffi-dev jpeg-dev zlib-dev
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
#RUN apk update && apk add build-essential libssl-dev libffi-dev
RUN mkdir /Code
WORKDIR /Code
COPY ./requirements.txt .

RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1

COPY . /Code/

ENTRYPOINT python /Code/core/manage.py runserver 0.0.0.0:8000

如何连接到django设置数据库以运行postgres容器?

非常感谢你

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51222
 
565 次点击  
文章 [ 2 ]  |  最新文章 4 年前