Hello everyone, This article is part of The Swarm series, The knowledge in this series is built in sequence, Check-out The Swarm series section above.
In This article We will complete The Swarm tutorials, Will Deploy a Stack consists of Nginx and MySql containers, Also will explain the values behind the usage of stack for deployment, in this lab will use Play-with-docker lab, Docker Stack commands, and compose YAML file.
Docker Stack command is used for Managing Swarm stacks, The Stack use to run multi-container Docker applications. With Stack you use a YAML file to define your Containers configurations, Then you use a single command to start and run The Stack from this YAML file.
It's the same concept As the Docker-compose command with normal docker mode, However we're using Docker Stack command with Swarm mode.
Defining all stack containers and configuration at one YAML file -Docker-compose file- The value of using docker stack and compose is the same value of using blueprints. Blueprints -which is YAML file in our case- is very useful for history. Instead of Saving you commands of the running containers and it's configurations at some files out-there, You use all of stack containers and configuration at one file and simply publish it with one single command.
And when you want to update your stack configuration, instead of save the updated commands at The File and the hassle of remembering which one to use in future, Simply you update the Same YAML file and run with The stack command. So you have one single file consist of the updated configurations.
Let's start out by creating our stack YAML file:
- Start by Build the past Swarm environment Here with out deploy any applications.
- On the manager node, Create a file called
docker-stack.yamlby the command below:
- Past the below content in the YAML file.
version: '3' services: # services list nginx: # service name image: nginx:latest # specify an image with it's tag ports: # defining ports - "8080:80" volumes: # mount volume disk | mount nginx.conf stored on local device to nginx container - ./nginx.conf:/etc/nginx/nginx.conf # establish connection to mysql container by mention the defined variables at mysql environment below environment: MYSQL_HOST: mysql MYSQL_PORT: 3306 MYSQL_DATABASE: myapp MYSQL_USER: root MYSQL_PASSWORD: password # The build of this container in depends on mysql depends_on: - mysql mysql: image: mysql:latest volumes: - ./data:/var/lib/mysql # Define mysql variables environment: MYSQL_DATABASE: myapp MYSQL_USER: root MYSQL_PASSWORD: password MYSQL_ROOT_PASSWORD: password
- Now you're ready to deploy your stack
- Run the following command to run
docker stack deploy -c docker-stack.yaml myapp
- The result for this command will create a network for the stack, the nginx service and the mysql service.
- list docker stack by the following:
docker stack ls
- list Container Services by the following:
# stack services docker stack services myapp # all swarm services docker service ls
- Get more info with
docker stack ps myapp
Now let's delete our environment with single & simple command:
- delete by the following:
docker stack rm myapp
At the end Note that the deployment went through Error, And the container cannot start; And Will discover how to troubleshoot in the next article....
That's it, Very straightforward, very fast🚀.
Hope this article inspired you and will appreciate your feedback. Thank you.