无法从Dockerized应用连接到Docker中的Postgres服务器 [英] Could not connect to postgres server in a docker from a dockerized app

查看:99
本文介绍了无法从Dockerized应用连接到Docker中的Postgres服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有dockerized postgres的dockerized Django应用程序。

I would like to run a dockerized Django app with a dockerized postgres.

我使用以下命令来运行dockerized Django应用程序:

I run the dockerized Django app by using:

docker run --rm --env-file /path/to/variables -d -p 8000:8000 django_app:test

我使用以下命令运行dockerized postgres:

I run a dockerized postgres by using:

docker run --rm -d --env-file /path/to/secrets/variables -p 5432:5432 \
    -v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf \
    --mount src=/path/to/db/data,dst=/var/lib/postgresql/data,type=bind \
    postgres:alpine -c 'config_file=/etc/postgresql/postgresql.conf'

我的postgres配置是建议的默认配置在 postgres Docker容器文档中。它实际上是一个配置文件,其中包含 listen_addresses ='*'

my postgres config is the default config that is suggested in the postgres docker container documentation. It is essentially a config file that contains listen_addresses = '*'

我使用的是相同环境变量:

DJANGO_SETTINGS_MODULE=settings.module
PROJECT_KEY=xxyyzzabcdefg
DB_ENGINE=django.db.backends.postgresql
POSTGRES_DB=db_name
POSTGRES_USER=db_user
POSTGRES_PASSWORD=verydifficultpassword
POSTGRES_HOST=localhost # I've also tried to use 0.0.0.0
POSTGRES_PORT=5432

该数据库的Django设置模块为:

My Django settings module for the database is:

 DATABASES = {
    'default': {
        'ENGINE': os.environ.get('DB_ENGINE'),
        'NAME': os.environ.get('POSTGRES_DB'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': os.environ.get('POSTGRES_HOST'),
        'PORT': os.environ.get('POSTGRES_PORT')
        }
  }

但是,我不断得到:

django.db.utils.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?

我的django应用程序的Dockerfile如下:

The Dockerfiles for my django app looks like:

FROM python:alpine3.7
COPY --from=installer /app /app
# required for postgres
COPY --from=installer /usr/lib /usr/lib
COPY --from=installer /usr/local/bin /usr/local/bin
COPY --from=installer /usr/local/lib /usr/local/lib
ARG SETTINGS_MODULE
WORKDIR /app
ENTRYPOINT python manage.py migrate &&\
           python manage.py test &&\
           python manage.py create_default_groups &&\
           python manage.py set_screen_permissions &&\
           python manage.py create_test_users &&\
           python manage.py init_core &&\
           python manage.py runserver 0.0.0.0:8000

另一个有趣的事实是,如果我在本地运行$ python manage.py runserver 的应用程序并运行postgres容器,则该应用程序似乎s可以正常工作。

Another interesting fact is that if I run the app locally python manage.py runserver and have the postgres container running, the app seems to be working.

有人可以帮我弄清楚为什么我的连接被拒绝了吗?

Can anyone help me try to figure out why am I getting a connection refused? Thanks in advance!

推荐答案

只需使用用户定义的桥接网络即可。首先,通过阅读有关Docker中不同类型网络的简短说明来利用您的知识: https:// docs .docker.com / network / bridge /

Just make use of user-defined bridge networking. First, leverage your knowledge with reading a short explanation of different types of networks in Docker: https://docs.docker.com/network/bridge/

第二,定义您自己的网络

Second, define your own network

docker network create foo

下一步,运行容器已连接到该网络:

Next, run your containers connected to this network:

docker run --rm --env-file /path/to/variables -d --network foo django_app:test
docker run --rm -d ... --network foo postgres:alpine ...

在两个命令-network foo 中都应注意。同样,在这种情况下,您不需要公开端口-在用户定义的网络内,端口是自动完成的:

Notice in both commands --network foo. Also you dont need to expose ports in this case - inside user-defined networks it is done automatically:


连接到同一个用户定义的桥网络
的容器会自动将所有端口彼此公开,而没有端口通向
外部世界。这使容器化的应用程序可以轻松地彼此通信
,而不会意外打开对
的外部访问。

Containers connected to the same user-defined bridge network automatically expose all ports to each other, and no ports to the outside world. This allows containerized applications to communicate with each other easily, without accidentally opening access to the outside world.

第三,使用-名称栏

docker run ... --network foo --name my-django django_app:test ...
docker run ... --network foo --name my-postgres postgres:alpine ...

最后修复连接字符串-从 localhost 更改到容器名称,例如 my-postgres

And finally fix the connection string - change from localhost to container name, like my-postgres:

...
POSTGRES_HOST=my-postgres
...

这篇关于无法从Dockerized应用连接到Docker中的Postgres服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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