Следуя теме этого вопроса , я пытаюсь запустить 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?
Но docker -db на этом IP действительно подключен.
Это журналы контейнера 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
Чего мне не хватает