Stacks are one of the most powerful orchestration features available in Docker Swarm. They allow you to easily manage complex applications consisting of multiple interdependent components running in separate containers.
In this lab, you will have the opportunity to work with Docker stacks by building a multi-component application as a Docker stack. You will also learn how to manage existing stacks by scaling a stack’s services after it has already been deployed. This will give you some hands-on insight into Docker stacks.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Build and deploy the application stack.
Create an empty project directory with a Docker compose YAML file inside.
cd ~/ mkdir produce cd produce vi produce.yml
Build a stack definition in
produce.yml
to meet the provided specifications.version: '3' services: fruit: image: linuxacademycontent/fruit-service:1.0.1 vegetables: image: linuxacademycontent/vegetable-service:1.0.0 all_products: image: linuxacademycontent/all-products:1.0.0 ports: - "8080:80" environment: - FRUIT_HOST=fruit - FRUIT_PORT=80 - VEGETABLE_HOST=vegetables - VEGETABLE_PORT=80
Deploy the stack using the compose file.
docker stack deploy -c produce.yml produce
Verify that the stack is working.
curl localhost:8080
Note that after deploying, it may take a few moments for the stack to become responsive. You can check the status of the services with
docker stack services produce
. Once the services are up and running, you should get some JSON data containing a combined list of fruits and vegetables.
- Scale the Fruit and Vegetable services in the stack.
Set the number of replicas to
3
for the Fruit and Vegetable services in the compose file.vi produce.yml
version: '3' services: fruit: image: linuxacademycontent/fruit-service:1.0.1 deploy: replicas: 3 vegetables: image: linuxacademycontent/vegetable-service:1.0.0 deploy: replicas: 3 all_products: image: linuxacademycontent/all-products:1.0.0 ports: - "8080:80" environment: - FRUIT_HOST=fruit - FRUIT_PORT=80 - VEGETABLE_HOST=vegetables - VEGETABLE_PORT=80
Redeploy the stack using the compose file.
docker stack deploy -c produce.yml produce
Verify that the stack is still working.
curl localhost:8080
You should get some JSON data containing a combined list of fruits and vegetables.
Use
docker stack services produce
to see that the number of replicas for the Fruit and Vegetable services is now 3.