Overlay Networks
Overlay networks are an abstraction that make it possible for nodes on otherwise disconnected networks to communicate as if they are on the same network. Turns out their is a way to do this with docker networking so you can run your services from a docker compose on different machines without them having to know about it. The services can all continue to talk to each other using the docker DNS names without concern for where the other services are actually located!
Setting up a Docker Swarm
In order to set up an overlay network, even for non-swarm containers, you first have to create a swarm with each of the machines you want to deploy to. Luckily this is pretty simple, assuming you already have docker on each machine.
Step 1:
First run this on the machine you want to be the manager node:
docker swarm init --data-path-port <port>
--data-path-port <port>
: In order to get the networking between the nodes working in my case, I had to override the default port as it was conflicting with other ports on the machines for some reason, you may find it works without this.
Note: If you have trouble getting the swarm to work, there are a few ports that docker swarm operates on. Make sure these are open in any firewall between your machines as mentioned here: Required Ports
Step 2:
The previous command will output a new command including a token. Copy and paste this command into your other nodes/machines to join them to the swarm.
On the manager node, you can run docker node ls
to list the nodes in the swarm. You should see each machine you added.
Step 3:
Once you have a docker swarm set up, any overlay network created on the manager will be accessible from any of the worker nodes. I will show an example of how I integrated the creation of this overlay network and the connection of the nodes on the other machine in the following section.
Docker Compose
I used a single docker compose file to deploy different sections to different machines using profiles. You could also create different compose files. Learn More about Compose Profiles
I created two profiles, manager and worker, and put each service in the profile where I wanted them to run:
Profiles
|
|
Node 2 runs on the manager, node 3 runs on the worker
Github Action Example
|
|
Compose Network Configuration
|
|
The w-overlay is created on the manager as an ‘overlay’ network. attachable: true
is required for standalone containers to be able to ‘attach’ to the network.
The w-overlay-worker is used in nodes deployed to the worker machine. external: true
tells this node that the network should already exist, and not to work about creating it. This will connect to the network created by the manager.