Docker Swarm Still Rocks
Undeprecation Warning 🚨¶
This website is undeprecated.
It has been updated with some current recipes, because Docker Swarm still rocks. 😎
See Docker Swarm Mode or Kubernetes for more information.
If you want to see alternative resources, you could check the awesome-swarm list for more resources about Docker Swarm Mode. 🤓
Why?¶
Docker is a great tool (the "de facto" standard) to build Linux containers.
Docker Compose is great to develop locally with Docker, in a replicable way.
Docker Swarm Mode is great to deploy your application stacks to production, in a distributed cluster, using the same files used by Docker Compose locally.
So, with Docker Swarm Mode you have:
- Reproducibility, use the same files as when developing locally.
- Simplicity and speed for development and deployment.
- Robustness and security, with fault-tolerant clusters.
Docker Swarm mode¶
If you have Docker installed, you already have Docker Swarm, it's integrated into Docker.
You don't have to install anything else.
Note¶
Whenever you read here "Docker Swarm" we are actually talking about "Docker Swarm mode".
Not the deprecated product called "Docker Swarm".
Alternatives¶
Some of the main alternatives are:
- Kubernetes.
- Mesos.
- Nomad.
To use any of them you need to learn a huge new set of concepts, configurations, files, commands, etc.
About Docker Swarm mode¶
Docker Swarm mode is comparable to them.
But it has simpler concepts and its requirements are very low. You can easily set it up on any VPS running Docker.
Take things back into your own hands! Docker Swarm is a very good fit, if you just want to deploy some containers.
Try it.
Set up a distributed cluster ready for "production".
Hint: Distributed cluster, does not mean high availability.
...In about 20 minutes.
If it doesn't work for you, then you can go for Kubernetes, Mesos or any other.
Those are great tools. But learning them might take weeks. So, the 20 minutes spent here are not much (and up to here you already spent 3 minutes).
Single server¶
With Docker Swarm mode you can start with a "cluster" of a single server.
You can set it up, deploy your applications and do everything on a $5 USD/month server.
And then, when the time to grow comes, you can add more servers to the cluster.
With a one-line command.
And you can create your applications to be ready for massive scale from the beginning, starting from a single small server.
About Docker Swarm Still Rocks¶
This is not associated with Docker or any of the tools suggested here.
It's mainly a set of ideas, documentation and tools to use existing open source products efficiently together.
Huge thanks to @tiangolo who originally created dockerswarm.rocks.
Prerequisites¶
- To know some Linux.
- To know some Docker.
Install and set up¶
Install a new Linux server with Docker¶
- Create a new remote VPS ("virtual private server").
- Deploy the latest Ubuntu LTS ("long term support") version. At the time of this writing it's
Ubuntu 24.04
. - Enable root or at least sudo access to it and enable password-less SSH key access.
Tip: You can copy your own key to it, by using
ssh-copy-id root@your-cheapo-vps-ip
- install latest Docker on it, Community Edition suffices
Tip: You can do this easily by using Ansible.
- Get either a brand-new domain that still rocks (e.g.
dockerswarmstill.rocks
) or have a subdomain resolve toyour-cheapo-vps-ip
(e.g.168.119.115.151
).
That's basically the only you ever do, directly on your VPS (apart from updates, of course!).
Note: The following is assuming, that there is no firewall at all in place on your VPS. Which is fine, if you are only exposing HTTP services.
Setup remote docker access¶
From now on, you will be remotely controlling the running Docker engine on your VPS by your locally running (e.g. on your machine) Docker (client) CLI. You can control any Docker engine, by setting the DOCKER_HOST
environment variable, but it is much more convenient to use Docker contexts:
docker context create dockerswarmstillrocks --docker host=ssh://root@dockerswarmstill.rocks
docker context ls
docker context use dockerswarmstillrocks
Set up swarm mode¶
In Docker Swarm Mode you have one or more "manager" nodes and one or more "worker" nodes. You init
the first manager node and have other nodes join
the swarm. You can control the whole swarm via remotely controlling on manager node. So make sure, that you are using the proper context.
The first step is to configure one (or more) manager nodes.
docker swarm init
Note: if you see an error like:
Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on interface eth0 (198.51.100.48 and 10.19.0.5) - specify one with --advertise-addr
...select the public IP (e.g. 198.51.100.48
in this example), and run the command again with --advertise-addr
, e.g.:
docker swarm init --advertise-addr 198.51.100.48
Add manager nodes (optional)¶
- Using the
dockerswarmstillrocks
context, for each additional manager node you want to set up, run:
docker swarm join-token manager
- Copy the result and paste it in the additional manager node's terminal, it will be something like:
docker swarm join --token SWMTKN-1-5tl7yaasdfd9qt9j0easdfnml4lqbosbasf14p13-f3hem9ckmkhasdf3idrzk5gz 192.0.2.175:2377
Tip: Keep it simple first, if you want to distribute load, start with one manager and one worker node, add worker nodes as needed.
Add worker nodes (optional)¶
- Using the
dockerswarmstillrocks
context, for each additional worker node you want to set up, run:
docker swarm join-token worker
- Copy the result and paste it in the additional worker node's terminal, it will be something like:
docker swarm join --token SWMTKN-1-5tl7ya98erd9qtasdfml4lqbosbhfqv3asdf4p13-dzw6ugasdfk0arn0 192.0.2.175:2377
Tip: Do not bother setting up docker contexts on worker nodes. You only need them to join the swarm only once.
Check it¶
- Check that the cluster has all the nodes connected and set up:
docker node ls
It outputs something like:
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
cck5z4w7f3yr74yrdvdtn9ogs * dockerswarmstillrocks-4gb-nbg1-1 Ready Active Leader 28.2.2
My first stack¶
In Docker Swam you deploy
stacks that consist of multiple containers, that are named services. Any service can publish a port on either overlay networks or directly on the host that the Docker engine is running. Each host-published port is automagically routed between swarm members.
Tip: Don't you worry! You'll see in a minute, how things work together!
So let's deploy a simple container! We will be using traefik/whoami for that purpose:
- Download the file stacks/whoami/docker-compose.yml:
curl -L dockerswarmstill.rocks/stacks/whoami/docker-compose.yml -o whoami.yml
- ...or create it manually, for example, using
nano
:
nano whoami.yml
- And copy the contents inside:
services:
whoami:
image: traefik/whoami:latest
ports:
- "1337:80"
whoami
stack
docker stack deploy -c whoami.yml whoami
Tip: Make sure, you are controlling your Docker Swarm manager.
- See the services of stack
whoami
:
docker stack ps whoami
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
c0ohzlts5x1x whoami_whoami.1 traefik/whoami:latest dockerswarmstillrocks-4gb-nbg1-1 Running Running 2 minutes ago
dockerswarmstill.rocks
) or the IP (e.g. 168.119.115.151
) of your VPS, so give it a try:
curl dockerswarmstill.rocks:1337
Hostname: c8382051c434
IP: 127.0.0.1
IP: ::1
IP: 10.0.0.163
IP: 172.18.0.4
IP: 10.0.2.3
RemoteAddr: 10.0.0.2:54782
GET / HTTP/1.1
Host: dockerswarmstill.rocks:1337
User-Agent: curl/8.7.1
Accept: */*
Done¶
That's it. Deployed! How about running the same thing locally?
docker context ls
docker context use desktop-linux
docker compose -f whoami.yml up
Tip: Your "local" Docker context might have a different name. Look it up in the above list of contexts.
You have a (distributed) Docker swarm mode cluster set up and deployed your very first stack, that you can run locally from the same file!
Note
You might want to rm
(oh no!) your freshly deployed whoami
stack with:
docker context use dockerswarmstillrocks
docker stack rm whoami
Check other sections in the documentation at https://dockerswarmstill.rocks to see how to set up HTTPS with a reverse proxy, you still have time, the 20 minutes are not over yet.
You already did the hard part, the rest is easy(TM).
Bonus: This website itself is hosted via Docker Swarm and build, deployed via GitHub actions, from a single docker-compose.yml if you are a little bit impatient (and still managed to read until the very end!)