0

无法从 docker 容器连接到 Postgres。我不希望用户 docker-compose 并创建一个 Postgres 容器,已经让 Postgres 应用程序运行。我认为容器 postgres 和更好地使用系统是一个坏主意。

OSX,Postgres10,烧瓶

我已经做了

postgresql.conf

listen_addresses = '*'  
port = 5432

pg_hba.conf

host    all             all             0.0.0.0/0               trust

我对任何结果都使用了信任,但没有效果。

Dockerfile

FROM python:3.8-alpine
RUN apk update \
    && apk add --virtual build-deps gcc musl-dev \
    && apk add python3-dev \
    && apk add postgresql-dev \
    && apk add jpeg-dev zlib-dev libjpeg \
    && pip install --upgrade pip
ENV PYTHONUNBUFFERED 1 \
    && FLASK_APP app.py
#EXPOSE 5000 5432
WORKDIR /app
ADD . /app
RUN pip install -r requirements.txt
CMD ["./bin/run.sh"]

./bin/run.sh

#!/bin/sh
#python run.py
source venv/bin/activate
set -e
flask db upgrade
exec gunicorn -b --workers 4 --access-logfile --error-logfile run:app :5000

我尝试使用的“docker run”命令:

docker run --rm -e SQLALCHEMY_DATABASE_URI=postgresql://postgres:postgres@0.0.0.0:5432/my_db --net=host -p 5000:5000 my_container:v0.1

该命令导致错误

...
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?

命令将我连接到 Postgres

psql -U postgres -h 0.0.0.0
4

1 回答 1

0

Trying to connect to host "0.0.0.0" is technically wrong. (the address 0.0.0.0 is used to other purposes no to connect to...)

Did you tried connect using the postgres container ip address instead of "0.0.0.0"?

You can get the postgres container ip address throught the following command:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' name_of_postgres_container
于 2020-02-26T23:25:16.143 回答