Docker:用户localhost而不是db连接到PostgresDatabase

时间:2018-11-23 08:07:33

标签: django docker

使用Docker时遇到问题。要将Django应用程序连接到Postgres数据库,我必须使用:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

但是,要在我的Pipenv Shell中通过pytest运行测试,我必须将“主机”从db更改为localhost。有什么方法可以随时使用本地主机吗?

Docker-Compose:

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/

0 个答案:

没有答案