Reputation: 636
Following the thread of this question, I am trying to run Django in one container, Postgres in another container, and connect them "manually" via user-defined networks.
I first create the containers:
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
This is my network after launching both containers:
[
{
"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": ""
}
},...
]
And this is the error on the Django container:
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?
But docker-db on that IP is indeed connected.
These are the logs of the postgres container:
(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
What am I missing?
Upvotes: 1
Views: 391
Reputation: 881
If two containers share the same network you can connect one to another using the container name
.
For example:
docker run -it --name this-is-postgres --network=django-network postgres
docker run -it --name this-is-django --network=django-network python3
Both containers have a shared network, called django-network
, and to get the resources contained in the postgress container you can use its container name: this-is-postgres
.
In django settings, for example:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "mydb",
"USER": "pg",
"PASSWORD": "Password123",
"HOST": "this-is-postgres", # the container name!
"PORT": 5432,
},
}
Edit:
if you can connect to the network the problem comes from a wrong configuration of the database and/or the database you are trying to connect does not exist. The following docker run
is compatible with the previous Django settings database configuration. The env variables are used to set username, password and to create a database called mydb
.
docker run -it --name this-is-postgres -e POSTGRES_PASSWORD=Password123 -e POSTGRES_USER=pg -e POSTGRES_DB=mydb --network=django-network postgres
For the complete list of commands/env variables see https://hub.docker.com/_/postgres
Upvotes: 1