![](/img/trans.png)
[英](psycopg2.OperationalError) could not connect to server: Connection refused Is the server
[英]Docker- django throws error while connecting to postgres: psycopg2.OperationalError: could not connect to server: Connection refused
我正在嘗試 dockerize 我的 Django-postgres 應用程序。 我的 Dockerfile 是:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
我的 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
我的 settings.py 有以下數據庫代碼:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': '0.0.0.0',
'PORT': '5432',
}
}
我的 db 和 web 容器已啟動,但是當我運行時: docker-compose 運行 web Z23EEEB4347BDD26BDD26B7pyEE9A出現錯誤:5DDFC6B7pyEE9A3
psycopg2.OperationalError:無法連接到服務器:連接被拒絕服務器是否在主機“0.0.0.0”上運行並接受端口 5432 上的 TCP/IP 連接?
如何讓我的容器進行通信?
將HOST
更改為:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': 'db',
'PORT': '5432',
}
}
0.0.0.0
不是有效的IP
,此外您需要使用service
名稱進行連接,因為compose
會為您解析
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.