[Answered ]-Docker-compose – Cannot connect to Postgres

1👍

I write this to save anyone in future from the same issue.

After countless tries, I started thinking that nothing was wrong from the pure docker perspective: I was right.

SOLUTION: My only suspect was related to the execution inside a Virtual Machine, so executing the same docker image on the host worked like a charm!
The networking issue was related to the VM (VirtualBox Ubuntu 20.04)

I do not know if there is a way to work with docker-compose inside a VM, so any suggestion is appreciated.

👤paolop

0👍

You said in a comment:

The command I run is the following: docker run -it --entrypoint /bin/sh backend_web

Docker Compose creates several Docker resources, including a default network. If you separately docker run a container it doesn’t see any of these resources. The docker run container is on the "default bridge network" and can’t use present-day Docker networking capabilities, but the docker-compose container is on a separate "user-defined bridge network" named backend_default. That’s why you’re seeing a couple of the symptoms you do: the two networks have separate IPv4 CIDR ranges, and Docker’s container DNS resolution only happens for the current network.

There’s no reason to start a container with an interactive shell and then start your application within that (any more than you don’t normally run python and then manually call main() from its REPL). Just start the entire application:

docker-compose up -d

If you do happen to need an interactive shell to debug your container setup or to run some manual tasks like database migrations, you can use docker-compose run for this. This honors most, but not all, of the settings in the docker-compose.yml file. In particular you can’t docker-compose run an interactive shell and start your application from it, since it ignores ports:.

# Typical use: run database migrations
docker-compose run web \
  ./manage.py migrate

# For debugging: run an interactive shell
docker-compose run web bash

Leave a comment