Today I Learned: Docker Compose Networks

Due to a bug fix or maybe a new feature to Docker Compose, I caught myself yak shaving how Docker and Docker Compose handle local networking.

The Problem

I have 2 services, let’s call them foo-service and bar-service. They both live in their respective directories as siblings.

.
├── bar-service
│   ├── docker-compose.yml
│   └── src
└── foo-service
    ├── docker-compose.yml
    └── src

bar-service refers to foo-service’s network like so:

# bar-service/docker-compose.yml

networks:
  default:
    external:
      name: fooservice_default

The Yak Shave

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
https://docs.docker.com/compose/networking/

The Solution

As of docker-compose 1.21.0:

Dashes and underscores in project names are no longer stripped out.
https://github.com/docker/compose/blob/master/CHANGELOG.md#1210-2018-04-10

This means that the docker-compose.yml file above for bar-service needs the network name for foo-service updated to add back in the stripped dashes.

# bar-service/docker-compose.yml

networks:
  default:
    external:
      name: foo-service_default

As of Docker Compose file format version 3.5, the network name can be explicitly defined as described in Specify custom networks.

# foo-service/docker-compose.yml

version: "3.5"

networks:
  default:
    name: fooservice

And the original external network configuration file for bar-service would now work, referencing the network of foo-service as fooservice_default.