[英]Error when connecting to postgres from a Python container
I'm trying to set up two communicating containers as different services我正在尝试将两个通信容器设置为不同的服务
Here's my Dockerfile
这是我的
Dockerfile
FROM python:3.8.8-slim-buster
RUN mkdir /project
WORKDIR /project
COPY . /project/
RUN apt-get update && \
apt-get install --no-install-recommends -y \
build-essential \
bash
RUN pip install --upgrade --no-use-pep517 \
pip \
setuptools \
wheel \
jupyterlab \
ipython-sql \
psycopg2-binary
EXPOSE 8888
and my docker-compose.yml
和我
docker-compose.yml
version: '3'
services:
db:
image: postgres:12.1-alpine
environment:
- POSTGRES_DB=ps_db
- POSTGRES_USER=ps_user
- POSTGRES_PASSWORD=ps_pass
ports:
- "5432:5432"
web:
build:
context: .
command:
jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser --allow-root
volumes:
- .:/project
ports:
- "8888:8888"
depends_on:
- db
Although I'm pretty sure my settings are fine, whenever I run虽然我很确定我的设置很好,但每当我运行
import psycopg2
conn = psycopg2.connect(database="ps_db", user="ps_user", password="ps_pass", host="0.0.0.0", port=5432)
I get this error我收到这个错误
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?
You need to set the 0.0.0.0 IP to 'db', because the postgres runs in another container called 'db', this is the name of your service.您需要将 0.0.0.0 IP 设置为 'db',因为 postgres 运行在另一个名为 'db' 的容器中,这是您的服务的名称。 So your conn will look like this:
所以你的 conn 看起来像这样:
conn = psycopg2.connect(database="ps_db", user="ps_user", password="ps_pass", host="db", port=5432)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.