无法使用心理备份2连接到Postgres容器 [英] Cannot connect to Postgres container using psycopg2

查看:26
本文介绍了无法使用心理备份2连接到Postgres容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

  • 容器中的一个简单的Flaskapp

  • 邮筒容器

使用以下Docker文件:

FROM python:alpine3.7

COPY . /app
WORKDIR /app

RUN apk update
RUN apk add --virtual build-deps gcc python3-dev musl-dev
RUN apk add postgresql-dev
RUN pip install -r requirements.txt
RUN apk del build-deps

EXPOSE 5006

CMD ["python", "./app.py"]

和数据库:

FROM postgres:11.1-alpine

COPY create_db.sql /docker-entrypoint-initdb.d/

EXPOSE 5432

This is my docker-compose YAML(将端口映射到主机以检查容器工作):

version: '3'
services:
    postgres:
        image: ccdb
        build: ccDb
        restart: always
        environment:
            POSTGRES_PASSWORD: password
        ports:
            - "5432:5432"

    top:
        image: ccpytop
        build: ccPyTop
        ports:
            - "5006:5006"

我可以从我的主机成功连接到数据库,并且我可以导航到由FlaskApp提供的应用程序页面。当我导航到某个页面时,应用程序会连接到数据库,因此Postgres容器有足够的时间启动。

当我运行docker-compose up时,我收到以下信息:

postgres_1  | 2018-12-04 09:45:13.371 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1  | 2018-12-04 09:45:13.371 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1  | 2018-12-04 09:45:13.377 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1  | 2018-12-04 09:45:13.398 UTC [19] LOG:  database system was interrupted; last known up at 2018-12-04 09:42:23 UTC
postgres_1  | 2018-12-04 09:45:13.521 UTC [19] LOG:  database system was not properly shut down; automatic recovery in progress
postgres_1  | 2018-12-04 09:45:13.524 UTC [19] LOG:  redo starts at 0/1680BC8
postgres_1  | 2018-12-04 09:45:13.524 UTC [19] LOG:  invalid record length at 0/1680CA8: wanted 24, got 0
postgres_1  | 2018-12-04 09:45:13.524 UTC [19] LOG:  redo done at 0/1680C70
postgres_1  | 2018-12-04 09:45:13.536 UTC [1] LOG:  database system is ready to accept connections

但当我在python容器中执行以下操作时

>>> import psycopg2
>>> conn = psycopg2.connect("host=/var/run/postgresql dbname=postgres user=postgres password=password")

我明白了:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
我尝试在没有主机的情况下运行,但收到相同的消息,使用/tmp/而不是/var/run/。我也尝试了链接容器,但我注意到这是一个遗留功能,而且无论如何都不起作用。

这是我第一次使用docker-compose,我几乎被困在这一点上了。有什么想法吗?

推荐答案

您正在运行两个独立的容器,这两个容器彼此无法识别,因此无法相互通信。

为了从一个容器到另一个容器进行通信,它们必须位于同一网络上。在Docker中,您可以创建自己的子网:

docker network create <network-name>

然后您可以通过添加

将容器添加到网络
--network=<network-name> --network-alias=<network-alias>

到容器的docker run命令。

然后,同一网络上的任何其他容器都可以通过其<network-alias>访问停靠容器。对您来说,这意味着

conn = psycopg2.connect("host=/var/run/postgresql dbname=postgres user=postgres password=password")

更改为

conn = psycopg2.connect("host=<network-alias> dbname=postgres user=postgres password=password")

其中network-alias是您为postgres容器指定的别名。

编辑

查看docker-compose networking以获取如何在docker-compose中执行此操作的手册。 显然,Docker-Compose在创业之初就建立了自己的网络。如果您的docker-compose位于directory,那么docker-compose将创建一个网络directory_default,在该网络上可以通过您在docker-compose文件中指定的名称发现不同的容器。因此,如果您将您的python命令更改为

conn = psycopg2.connect("host=postgres dbname=postgres user=postgres password=password")

它应该可以工作。

这篇关于无法使用心理备份2连接到Postgres容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆