Managing and Scaling the Docker Swarm Stack
Once you have deployed your multi-service application as a Docker Swarm stack, you can use various commands to manage and scale it.
Deploying the Stack
To deploy your application as a Docker Swarm stack, you can use the docker stack deploy
command:
docker stack deploy -c docker-compose.yml my-app
This will create the Swarm services based on the configurations in your docker-compose.yml
file.
Monitoring the Stack
You can use the following commands to monitor the status of your Swarm stack:
## List the running services
docker stack services my-app
## List the tasks (container instances) for a service
docker service ps my-app_web
## View the logs for a service
docker service logs my-app_web
Scaling the Stack
To scale a service in your Swarm stack, you can use the docker service scale
command:
## Scale the "web" service to 5 replicas
docker service scale my-app_web=5
This will create 4 more instances of the web
service, and Docker Swarm will automatically load balance the traffic across them.
graph LR
client[Client] --> load_balancer[Load Balancer]
load_balancer --> web1[Web Service]
load_balancer --> web2[Web Service]
load_balancer --> web3[Web Service]
load_balancer --> web4[Web Service]
load_balancer --> web5[Web Service]
web1 --> db[Database Service]
web2 --> db
web3 --> db
web4 --> db
web5 --> db
Updating the Stack
To update your Swarm stack with a new version of a service, you can use the docker service update
command:
## Update the "web" service to use a new image
docker service update my-app_web --image labex/web-app:v2
This will rolling update the web
service, replacing the old containers with the new ones, without interrupting the service.
By using these commands, you can effectively manage and scale your Docker Swarm stack to ensure high availability and performance of your multi-service application.