๐ Deploying Core Service with Docker Swarm
The core/
backend service is deployed on a self-managed VPS using Docker Swarm โ a lightweight alternative to Kubernetes.
This guide explains the rationale, setup steps, and deployment process using docker stack
.
๐ง Why Docker Swarm?
While Kubernetes is a full-featured orchestration system, it's often overkill for small-scale or single-node production apps.
Docker Swarm was chosen because:
- โขโก Lightweight and simple
- โข๐ธ Lower resource usage than Kubernetes
- โข๐ Easily deployable on a single VPS
- โข๐ Supports rolling updates, blue-green deployment, and automatic rollback
- โข๐งฑ Uses
docker stack
โ similar to docker-compose
but production-ready
๐ฆ VPS Setup
Before deploying, we followed secure VPS setup practices including:
- โขSSH hardening
- โขFirewall setup
- โขUser creation with limited privileges
๐ Refer to this guide:
zenstats VPS Setup Docs
๐ณ Docker Swarm Deployment Steps
1๏ธโฃ Add Docker Context
Using docker context
, you can run remote Docker commands from your local machine:
docker context create devx --docker "host=ssh://parth@api.devx.parthkapoor.me"
docker context use devx
You can now use Docker CLI commands on the VPS as if running locally.
2๏ธโฃ Initialize Swarm
On your main manager node (your VPS):
โน๏ธ If you want to add more nodes, Docker will provide a token and command to join them.
3๏ธโฃ Initialize Swarm
Add Secrets using docker secrets
docker secret create spaces_access_key ./secrets/spaces_access_key.txt
docker secret create spaces_secret_key ./secrets/spaces_secret_key.txt
docker secret create redis_url ./secrets/redis_url.txt
docker secret create github_client_id ./secrets/github_client_id.txt
docker secret create github_client_secret ./secrets/github_client_secret.txt
docker secret create session_secret ./secrets/session_secret.txt
docker secret create kubeconfig_file ./secrets/kubeconfig.yaml
โน๏ธ If you want to add more nodes, Docker will provide a token and command to join them.
4๏ธโฃ Deploy Stack
Deploy your service using the docker-stack.yaml
file:
docker stack deploy -c docker-stack.yaml devex
This will spin up the defined services under the stack name devex
.
5๏ธโฃ Monitor Services
To list running services:
To inspect tasks of a specific service:
docker service ps devex_core
Replace core
with your specific service name from the docker-stack.yaml
.
๐ Stack File: docker-stack.yaml
Your stack file describes:
- โขServices (e.g.,
core
)
- โขVolumes
- โขEnvironment variables
- โขDeployment strategies (e.g., rolling updates)
- โขPort mapping
- โขSecrets (optional)
โ
Benefits of docker stack
over docker-compose
Feature | docker-compose | docker stack |
---|
Multi-host support | โ | โ
|
Rolling updates | โ | โ
|
Health checks + restart policies | ๐ Limited | โ
|
Secrets + configs | โ
(limited) | โ
|
Built-in orchestration | โ | โ
|
Scalable replicas | โ | โ
|
Auto rollback on failure | โ | โ
|
๐งน Cleanup
To remove the stack:
To leave swarm mode:
docker swarm leave --force
๐ฌ Notes
- โขYou can deploy multiple services via
docker-stack.yaml
, and manage them together.
- โขAdd load balancer (e.g., NGINX) if exposing to public internet with TLS (check k8s/cert-manager for ideas).
- โขFuture enhancement: integrate with GitHub Actions for CI/CD to auto-deploy on push.