django - Docker:使用 localhost 而不是 db 连接到 PostgresDatabase
问题描述
我在使用 Docker 时遇到了问题。要从 Django 应用程序连接到 Postgres 数据库,我必须使用:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
但是,要在我的 Pipenv shell 中通过 pytest 运行测试,我必须将 'Host': 从更改db
为localhost
. 有没有一种方法可以让我始终使用 localhost?
码头工人组成:
version: '3'
services:
db:
image: postgres
ports:
- "5432:5432"
web:
build: .
env_file: .env
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
container_name: test
Dockerfile:
# Pull base image
FROM python:3
# Set environment varibles
ENV PYTHONUNBUFFERED 1
# Set work directory
RUN mkdir /code
WORKDIR /code
# Install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /code/Pipfile
RUN pipenv install --deploy --system --skip-lock --dev
# Define ENTRYPOINT
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
# Copy project
COPY . /code/
解决方案
推荐阅读
- html - Pug 预处理器和缩进的问题
- javascript - 如何从 URL 中删除 accesstoken
- python - 有没有办法将秘密 python 模块与种子一起使用?
- html - div元素的边界矩形的奇怪行为
- php - 从本地 php 服务器查询时,AWS RDS Postgres 非常慢
- r - 字符变量的 order() 如何在 data.table 中工作?
- android - 在 Flutter 中获取值 TextEditingController 时出错
- r - R:计算所选列非空的不同ID
- ms-access - 列表字段字段不显示值
- python - 如何以特定方式转换我的数据框?