Docker Swarm Series: #4th Deploy a Stack to a swarm Cluster
Tags: docker, swarm
Published: 2023-07-03 05:44:48

--- series: Swarm ---
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dhscpvf2hb3mx334z13.png)
<br>



# Inception <br><br>

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.

At the [last article](https://dev.to/mohamedeleraki/docker-swarm-series-3th-create-a-highly-available-container-faa) we covered How to Deploy a highly available Container, And deployed a simple Nignx web-app using DockerCLI and [Play-with-docker](https://labs.play-with-docker.com/) lab.

----------------------------------------------------------
<br><br>
# Overview <br><br>

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](https://labs.play-with-docker.com/) lab, Docker Stack commands, and compose YAML file.<br>

----------------------------------------------------------
<br><br>
# What is Stack<br><br>

[Docker Stack command](https://docs.docker.com/engine/reference/commandline/stack/) 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.
<br><br>

## The value behind the Stack:<br>

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.


----------------------------------------------------------
<br><br>
# Example Docker Stack YAML file<br><br>

Let's start out by creating our stack YAML file:

- Start by Build the past Swarm environment [Here](https://dev.to/mohamedeleraki/docker-swarm-series-3th-create-a-highly-available-container-faa) with out deploy any applications.
- On the manager node, Create a file called `docker-stack.yaml` by the command below:
```bash
vim docker-stack.yaml
```
- Past the below content in the YAML file.
```bash
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

----------------------------------------------------------
<br><br>
# Stack deploy<br><br>

- Run the following command to run `myapp` stack:
```bash
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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2z15fo9qzpts91trl6fs.png)<br><br>



- list docker stack by the following:
```bash
docker stack ls
```<br>



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfg233nlcf5lslqke777.png)<br><br>

- list Container Services by the following:
```bash
# stack services
docker stack services myapp

# all swarm services
docker service ls
```<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkyki9m6k8vpr2ayz96y.png)<br>

- Get more info with `docker stack ps myapp` <br>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddqs0sz26vsaw2e4z1g1.png)<br><br>

----------------------------------------------------------
<br><br>
# Stack Delete<br><br>

Now let's delete our environment with single & simple command:
 
- delete by the following:

```bash
docker stack rm myapp
```<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/haynwbyi2vf66mikxthx.png)<br><br>

**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....**

----------------------------------------------------------
<br><br>


That's it, Very straightforward, very fast🚀. 
Hope this article inspired you and will appreciate your feedback. Thank you.


----------------------------------------------------------
<br><br>
# References<br><br>

- [Deploy a stack to a swarm](https://docs.docker.com/engine/swarm/stack-deploy/)
- [Docker Compose overview](https://docs.docker.com/compose/)
- [Docker stack](https://docs.docker.com/engine/reference/commandline/stack/)
- [Play with docker](https://labs.play-with-docker.com)