Logo

dev-resources.site

for different kinds of informations.

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

Published at
7/3/2023
Categories
docker
swarm
Author
mohamedeleraki
Categories
2 categories in total
docker
open
swarm
open
Author
14 person written this
mohamedeleraki
open
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

swarm Article's
30 articles in total
Favicon
Knowledgeable Agents with FalkorDB Graph RAG
Favicon
Building Swarm-based agents with AG2
Favicon
Quick tip: Using SingleStore with OpenAI's Swarm
Favicon
OpenAI Swarm: Exploring Lightweight Multi-Agent Orchestration
Favicon
Building an 🐝 OpenAI SWARM πŸ” Web Scraping and Content Analysis Streamlit Web App with πŸ‘₯ Multi-Agent Systems
Favicon
What’s an AI Agent and what are its current advantages and possible future?
Favicon
Docker Swarm
Favicon
Docker Swarm Series: #7th Advanced Managing config and secret objects
Favicon
Docker Swarm Series: #5th Troubleshooting
Favicon
Docker Swarm Series: #8th Publishing Modes
Favicon
DOCKER SWARM CLUSTER & NFS
Favicon
Docker Swarm Series: #6th Managing config and secret objects
Favicon
Docker Swarm: Simplifying Container Orchestration at Scale
Favicon
Docker Swarm Series: #4th Deploy a Stack to a swarm Cluster
Favicon
Docker Swarm Series: #3rd Deploy a highly available Container
Favicon
Docker Swarm Series: #1st Setup the Environment
Favicon
Docker in small scale production (Docker Swarm)
Favicon
How to export your complete Foursquare checkin history
Favicon
Deploy a high available etcd cluster using docker
Favicon
Time to say goodbye to Docker Swarm
Favicon
Understanding Docker, Docker Compose & Swarm
Favicon
Everything you need to know about Docker Swarm
Favicon
Zero Downtime Deployment with Docker Swarm
Favicon
Workflow on Docker Swarm
Favicon
Free docker cluster mesh with swarm and GCP
Favicon
Traefik v2 with Docker Swarm
Favicon
swarm-cronjob
Favicon
Docker Swarm Concepts, Tips, and Tricks for a Docker Beginner
Favicon
Deploying gitlab on Docker Swarm
Favicon
Adventure with Docker: Conflicts with UIDs of the container and the host

Featured ones: