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
- This ensures the service only runs on worker nodes.
πΉ Rolling Updates
- Update services without downtime.
docker service update --image nginx:latest webapp
- 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
Attach to a service:
docker service create --name db --secret db_password postgres:latest
- 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
Attach to a service:
docker service create --name web --config source=nginx_conf,target=/etc/nginx/nginx.conf nginx
πΉ Best Practices
- Use secrets for sensitive data (passwords, API keys).
- Use configs for application configuration files.
- Scale services carefully based on node capacity.
- Monitor Swarm health and service updates.
πΉ Hands-On Challenge
- Create a secret and attach it to a database service.
- Create a config file and attach it to a web service.
- Deploy the service with placement constraints.
- 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)