Over the last few days I’ve been working on solving a DevOps style problem. And all of the various bits and pieces I’ve been trying to string together have been in Docker containers. My (somewhat faulty, it now turns out) understanding was that one container should be able to talk to another container just like that!

A reference for the younger and/or foreign members of the audience

Alas, this isn’t quite true. But in digging around (and my, isn’t Docker Networking such a well served area?) I did come across a couple of very useful commands.

This first command will create a bridge network called localnet.

docker network create --driver bridge localnet

From the documentation:

…a bridge network uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network.

Usefully (as described further down) a bridge network provides DNS resolution between containers. This means that if you give your container a name, you can use that name in other containers connected to the same network to refer to that container.

Great! Brilliant! But I already have a running container that I would like to refer to from another container…?

The second useful command is this:

docker network connect localnet eventstore

This command attaches the running container eventstore to the network localnet, without needing a restart, of anything. Now I can connect to the services provided by eventstore using the name eventstore from any other containers connected to localnet.

N.B. It’s worth pointing out that when connecting to services within the containers on the host (e.g. your machine) you still use localhost or 127.0.0.1 (or ::1 if you’ve enabled ipv6).