All content below targets personal projects; please don’t use it this way in work environments.
Why I Used swarm Mode
Enabling swarm is mainly for the following reasons:
Starting projects with only
docker runmeans that when updating a service, the service port closes and restarts — unless each version uses a separate port. If every version’s port differs, we’d also have to design an algorithm to maintain the available port list, and facing scaling needs, this algorithm becomes even more complex. swarm’s benefit is seamless migration during upgrades, greatly reducing deployment cost. Compared withdocker run, it provides more comprehensive features.Using an orchestration system like Kubernetes can of course also achieve seamless upgrades, but for simple personal projects there’s no need for Kubernetes. swarm mode is small and already sufficient. Of course you can still choose minikube — I personally just chose swarm.
For concrete features see the official docs: Swarm mode overview
In a single-machine environment, after installing docker, docker swarm init enables it.
The docker-compose File
docker-compose official documentation
compose is a tool that can define and run multiple containers; the syntax is a yaml file:
1 | version: '2.0' |
When writing this file I suggest paying attention to the version number. Also, don’t over-rely on black magic. Simple port mapping and file mounting are enough. For overly complex features, think ahead about whether they’re necessary.
Deploying Applications and CI with docker stack
Normally after writing the compose.yaml config file, I don’t start directly with docker-compose — I operate with docker stack deploy:
1 | # specify the compose file to use and the stack name |
Continuous integration also becomes simplified thanks to docker stack. When deploying an application, I only need to replace the image version in the config file and re-deploy. Take this file as an example:
1 | version: '3' |
When writing CI/CD rules, all I consider is replacing the image position. Used in azure-pipeline,
sed can replace the specific line — covering both development and deployment. Personally I find this approach quite practical and simple.
1 | - script: | |
docker stack Limitations
If you read the compose file reference blog in detail, you’ll find some caveats: certain options are ignored when docker stack starts. For example: devices, links, restart, cap_add, … many config items are not supported.
Personally I suggest thinking twice before using these unsupported options; simple web applications shouldn’t depend on such services.
Summary
This article briefly introduced using docker’s swarm mode in personal projects — a lightweight CI/CD means,
with excellent compatibility with various pipeline syntaxes, fully capable of quick integration into existing projects.
If you still have usage questions, discuss them in the comments.