在线程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我遗漏了什么?
发布于 2020-03-16 05:56:15
如果两个容器共享同一个网络,您可以使用容器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,要获取postgress容器中包含的资源,可以使用它的容器名称: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设置数据库配置兼容。环境变量用于设置用户名、密码和创建名为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
https://stackoverflow.com/questions/60697557
复制相似问题