DEV Community

Yash Sonawane
Yash Sonawane

Posted on

Docker Series: Episode 23 β€” Docker Swarm Advanced: Services, Secrets & Configs πŸ”

Welcome back to the Docker series! After learning Docker Swarm basics and advanced networking, it’s time to dive deeper into production-ready Swarm setups. In this episode, we’ll cover advanced Swarm services, secrets management, and configuration handling.


πŸ”Ή Advanced Services in Swarm

  • Swarm services allow you to manage containers declaratively.
  • You can define:

    • Replicas for scaling
    • Placement constraints (e.g., specific nodes)
    • Update strategies for rolling updates

Example: Service with Constraints

docker service create --name webapp --replicas 3 --constraint 'node.role==worker' nginx
Enter fullscreen mode Exit fullscreen mode
  • This ensures the service only runs on worker nodes.

πŸ”Ή Rolling Updates

  • Update services without downtime.
docker service update --image nginx:latest webapp
Enter fullscreen mode Exit fullscreen mode
  • Use flags like --update-parallelism and --update-delay for controlled rollout.

πŸ”Ή Docker Secrets

  • Secrets are encrypted and stored in Swarm.
  • Example: Creating and using a secret for a database password
docker secret create db_password ./db_password.txt
Enter fullscreen mode Exit fullscreen mode

Attach to a service:

docker service create --name db --secret db_password postgres:latest
Enter fullscreen mode Exit fullscreen mode
  • Inside the container, the secret is available at /run/secrets/db_password.

πŸ”Ή Docker Configs

  • Store configuration files securely in Swarm.
  • Example: Nginx config
docker config create nginx_conf ./nginx.conf
Enter fullscreen mode Exit fullscreen mode

Attach to a service:

docker service create --name web --config source=nginx_conf,target=/etc/nginx/nginx.conf nginx
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Best Practices

  1. Use secrets for sensitive data (passwords, API keys).
  2. Use configs for application configuration files.
  3. Scale services carefully based on node capacity.
  4. Monitor Swarm health and service updates.

πŸ”Ή Hands-On Challenge

  1. Create a secret and attach it to a database service.
  2. Create a config file and attach it to a web service.
  3. Deploy the service with placement constraints.
  4. Perform a rolling update to a new image.

βœ… Next Episode: Episode 24 β€” Docker Compose + Swarm Integration: Multi-Host Deployments β€” combine Compose simplicity with Swarm orchestration for scalable deployments.

Top comments (0)