[英]Docker- django throws error while connecting to postgres: psycopg2.OperationalError: could not connect to server: Connection refused
I am trying to dockerize my Django-postgres app.我正在尝试 dockerize 我的 Django-postgres 应用程序。 My Dockerfile is:我的 Dockerfile 是:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
My docker-compose.yml is:我的 docker-compose.yml 是:
version: '3'
services:
web:
build: .
command: python app/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: password
and my settings.py has the following database code:我的 settings.py 有以下数据库代码:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': '0.0.0.0',
'PORT': '5432',
}
}
My db and web containers are up but when I run: docker-compose run web python manage.py migrate I am getting the error:我的 db 和 web 容器已启动,但是当我运行时: docker-compose 运行 web Z23EEEB4347BDD26BDD26B7pyEE9A出现错误:5DDFC6B7pyEE9A3
psycopg2.OperationalError: could not connect to server: Connection refused Is the server running on host "0.0.0.0" and accepting TCP/IP connections on port 5432? psycopg2.OperationalError:无法连接到服务器:连接被拒绝服务器是否在主机“0.0.0.0”上运行并接受端口 5432 上的 TCP/IP 连接?
How can I make my containers communicate?如何让我的容器进行通信?
change HOST
to:将HOST
更改为:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': 'db',
'PORT': '5432',
}
}
0.0.0.0
is not a valid IP
, beside you need to connect using the service
name since compose
will resolve it for you 0.0.0.0
不是有效的IP
,此外您需要使用service
名称进行连接,因为compose
会为您解析
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.