I have the following containers running:

$ docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                                              NAMES
b02d801f9aa3        development_mongo      "docker-entrypoint.s…"   7 hours ago         Up About an hour>27017/tcp                           compose_mongo
2903bb830ea7        development_postgres   "docker-entrypoint.s…"   7 hours ago         Up About an hour>5432/tcp                            compose_postgres
6ce3fe015a8f        development_elastic    "/usr/local/bin/dock…"   7 hours ago         Up About an hour>9200/tcp,>9300/tcp   compose_elastic

When I create/run a Docker container that has python manage.py makemigrations as entrypoint, the Django app can't connect to the Postgres container:

 $ docker run --name test1 repo/myapp:stage_1605191501

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/base.py", line 217, in ensure_connection
  File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.6/dist-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection
connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.6/dist-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 "" and accepting
    TCP/IP connections on port 50000?

Why it cannot connect, since that Postgres port is already exposed to the host? What am I missing?

  • Please show your docker-compose.yml file. – Michael Hampton May 17 '19 at 4:40
  • I already got the problem, Michael: different networks. Some containers using a custom network and other container connected only to default bridge. I posted the answer below. Thanks :)! – ivanleoncz May 17 '19 at 4:42

Basically, all compose_* containers, are attached to a different Docker Network, as well as test1, is associated to another network.

$ docker network inspect development_default | grep 'Name\|IPv4'
    "Name": "development_default",
            "Name": "compose_postgres",
            "IPv4Address": "",
            "Name": "compose_elastic",
            "IPv4Address": "",
            "Name": "compose_mongo",
            "IPv4Address": "",

These containers were created through Docker Compose, without specifying a custom network. If no custom network is defined on your Docker Compose file, docker will create a custom network which uses the name of the directory from were you have your build environment. That's why the development, name of the directory which was holding the files for generating these containers, was used when creating a development_default network for them.

  • When executed the command $ docker run --name test1 repo/myapp:stage_1605191501, no --network was specified, so test1 will be associated to the default bridge network.

  • compose_postgres has 5432 port exposed to the host network and it is associated to development_default network (

Answer (steps):

  1. provide static IP for compose_postgres
  2. define this IP for the Django app definitions, inside the image of test1, in order to provide connection from the app to compose_postgres database
  3. define development_default as the --network for test1 when running docker run

For further reference:

  • You don't need the compose_ part, that's the project name in compose which can change. Set it to postgres, elastic, and mongo for the hostnames instead. You'll see network aliases configured for these when deploying your project in compose. – BMitch May 17 '19 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.