Sirikon's Neocities

0007 Mixing networks in Docker Compose

Turns out Docker Compose is pretty lazy and rigid when dealing with networks.

If you create a Docker container and join it to a bridge (default) network, the container's ID and name will resolve to the container's IP address in Docker's internal DNS server.

On top of that, Docker Compose will add the service name to the DNS server. No matter what. hostname doesn't replace the service name, just adds another name that resolves to the same thing. I can point you to the exact line in the code.

All these DNS names are added to every network the container joins, and you can't prevent that.

And that makes a funny scenario if you're trying to connect two projects. Imagine a escenario with these services:

If you connect beta's api service to alpha's network and alpha's web makes a request to api, who will respond? Well, both apis from alpha and beta are registered at the DNS name api.

To prevent any problems, you would need to create a new network, include both apis to it with aliases like alpha.api and beta.api and make sure that alpha's api is doing requests to the name beta.api, so it doesn't potentially call to itself. This way, you need to be aware of the potential name collissions in just one service, not many.

In summary: To connect two services from two different projects, create a third network and join just these two services, or confusing shit will happen.