core/DEPLOYMENT.md

๐Ÿš€ 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:
bash
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):
bash
docker swarm init
โ„น๏ธ 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
bash
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:
bash
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:
bash
docker service ls
To inspect tasks of a specific service:
bash
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

Featuredocker-composedocker 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:
bash
docker stack rm devex
To leave swarm mode:
bash
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.