DEV Community

Mohamed El Eraky
Mohamed El Eraky

Posted on • Updated on

Docker Swarm Series: #4th Deploy a Stack to a swarm Cluster

Image description

Inception

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 we covered How to Deploy a highly available Container, And deployed a simple Nignx web-app using DockerCLI and Play-with-docker lab.


Overview

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.


What is Stack

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.


The value behind the Stack:

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.


Example Docker Stack YAML file

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.yaml by the command below:
vim docker-stack.yaml
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Now you're ready to deploy your stack

Stack deploy

  • Run the following command to run myapp stack:
docker stack deploy -c docker-stack.yaml myapp
Enter fullscreen mode Exit fullscreen mode
  • The result for this command will create a network for the stack, the nginx service and the mysql service.

Image description

  • list docker stack by the following:
docker stack ls
Enter fullscreen mode Exit fullscreen mode

Image description

  • list Container Services by the following:
# stack services
docker stack services myapp

# all swarm services
docker service ls
Enter fullscreen mode Exit fullscreen mode

Image description

  • Get more info with docker stack ps myapp

Image description


Stack Delete

Now let's delete our environment with single & simple command:

  • delete by the following:
docker stack rm myapp
Enter fullscreen mode Exit fullscreen mode

Image description

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.


References

Top comments (0)