[英]manage.py test error in Django involving django.db.utils.OperationalError
I receive the following error when I run python manage.py test 运行python manage.py test时收到以下错误
django.db.utils.OperationalError: could not translate host name "db" to address: nodename nor servname provided, or not known django.db.utils.OperationalError:无法将主机名“ db”转换为地址:提供的节点名或服务名,或者未知
My docker-compose.yml looks like this: 我的docker-compose.yml看起来像这样:
version: '3'
services:
db:
image: postgres
ports:
- "5432:5432"
web:
entrypoint: /entrypoint.sh
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
My dockerfile looks like 我的dockerfile看起来像
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
Please give me any insight on how to resolve this issue. 请给我有关如何解决此问题的任何见解。
It's because they are not in the same network, 这是因为他们不在同一个网络中,
You should create a network and include both db and web in it, 您应该创建一个网络,并在其中包含数据库和网络,
version: '3'
services:
db:
image: postgres
ports:
- "5432:5432"
networks:
- backend
web:
entrypoint: /entrypoint.sh
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
networks:
- backend
networks:
backend:
driver: bridge
Be careful NOT TO EXPOSE PORTS FOR THE DB . 注意不要暴露数据库的端口 。
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: <password>
POSTGRES_USER: postgres
networks:
- backend
In your Django settings.py, you can now use db
as your hostname instead of localhost. 在Django settings.py中,您现在可以使用
db
作为主机名,而不是localhost。
DATABASES = {'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': '<password>',
'HOST': 'db',
'PORT': '5432',
}}
Your web
service stays as it is because this one needs the container's port 8000 bound to your localhost:8000
您的
web
服务保持不变,因为该服务需要绑定到localhost:8000
的容器的端口localhost:8000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.