将Django-Postgresql与独立Docker链接

问题描述 投票:0回答:1

跟随线程of this question,我试图在一个容器中运行Django,在另一个容器中运行Postgres,并通过用户定义的网络“手动”连接它们。

我首先创建容器:

docker run --rm --name postgres-10 \
    -d -it \
    -p 5432:5432 \
    --network django-network \
    postgres:10

docker run --rm --name betcomm-django \
    -d -it  \
    -p 8000:8000 \
    -v $PWD:/app/backend \
    --network django-network \
    --link postgres-10:docker-db \
    pablorr10/betcomm-django:dev

这是启动两个容器后的网络:

[
    {
        "Name": "django-network",
        "Id": "bbae9d656ea9ccc56c2c0f4db310d53fa135275358b16bc08636cf0c1a56127f",
        "Created": "2020-03-15T17:21:48.405473202Z",
        ...

        "Containers": {
            "79c7a66a5f4a892486d3c1d3089ff5850e2753af66cc694798015f80021297f3": {
                "Name": "postgres-10",
                "EndpointID": "d5fb80edfdf19678890da858e25e8bafd3e56113f82a5c1e39de5ca16f1caf5a",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            },
            "8873f054b23bb888b91deb64c819c1bf370db6ddc7b9f638ee60d850ee082da6": {
                "Name": "betcomm-django",
                "EndpointID": "e60640ec4dfbc1dd86b3464260ed73446846b8dd33b6e4b173befe26d5ee0738",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            }
        },...
]

这是Django容器上的错误:

root@8873f054b23b:/app/backend# python manage.py runserver 0.0.0.0:8000
Running Docker locally, setting env variables...

DB:  betcomm-dev
User:  postgres
Pass:  postgres
Host:  docker-db
Port:  5432
Running Docker locally, setting env variables...

DB:  betcomm-dev
User:  postgres
Pass:  postgres
Host:  docker-db
Port:  5432
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    ...
    ...
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
        Is the server running on host "docker-db" (172.18.0.2) and accepting
        TCP/IP connections on port 5432?

但是该IP上的docker-db确实已连接。

这些是postgres容器的日志:

(django) ➜  backend git:(serializing-on-kubernetes) ✗ docker logs postgres-10
2020-03-15 20:39:09.678 CET [1] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-03-15 20:39:09.679 CET [1] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
2020-03-15 20:39:09.679 CET [1] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2020-03-15 20:39:09.686 CET [1] LOG:  could not bind IPv4 address "192.168.65.2": Cannot assign requested address
2020-03-15 20:39:09.686 CET [1] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2020-03-15 20:39:09.686 CET [1] WARNING:  could not create listen socket for "docker.for.mac.localhost"
2020-03-15 20:39:09.688 CET [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-03-15 20:39:09.808 CET [26] LOG:  database system was shut down at 2020-03-15 20:38:56 CET
2020-03-15 20:39:09.843 CET [1] LOG:  database system is ready to accept connections

我想念什么?

django postgresql docker networking docker-networking
1个回答
0
投票

如果两个容器共享相同的网络,则可以使用容器name将一个连接到另一个。

例如:

docker run -it  --name this-is-postgres --network=django-network  postgres
docker run -it  --name this-is-django --network=django-network  python3

两个容器都有一个共享的网络,称为django-network,要获取包含在后端容器中的资源,可以使用其容器名称:this-is-postgres

例如,在django设置中:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "mydb",
        "USER": "pg",
        "PASSWORD": "Password123",
        "HOST": "this-is-postgres", # the container name!
        "PORT": 5432,
    },
}

编辑:

如果可以连接到网络,则问题出在数据库配置错误和/或您要连接的数据库不存在。以下docker run与先前的Django设置数据库配置兼容。 env变量用于设置用户名,密码并创建一个名为mydb的数据库。

docker run -it  --name this-is-postgres -e POSTGRES_PASSWORD=Password123 -e POSTGRES_USER=pg -e POSTGRES_DB=mydb --network=django-network  postgres

有关命令/环境变量的完整列表,请参见https://hub.docker.com/_/postgres

© www.soinside.com 2019 - 2024. All rights reserved.