docker swarm stack file Explained – Beginner Level4 min read

Prabhin Prabharkaran Administrator
DevOps Engineer

He is a Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!

follow me

Docker swarm is a docker orchestration tool that comes along with docker engine when helps to manage multiple Docker containers across multiple hosts that are joined to the cluster.
Docker Swarm enables high availability for docker containers by increasing the replicas for a particular service.

if you don’t know what is the difference between docker-compose file vs docker stack check out this article. docker-compose and docker stack


In this article, we are trying to explain about docker swarm stack file. We are splitting this article into two levels one is beginner level and another one is advanced.

 

version: '3.3'
services:
  nginx:
    image: nginx:latest
    ports:
     - 80:80
    volumes:
     - /opt/nginx.conf:/etc/nginx/nginx.conf
    networks:
     - default
    logging:
      driver: json-file
    deploy:
      replicas: 1
networks:
  default:
    external: true

 

The above stack file is for an nginx application, now let’s look into the stack file one by one.

version : ‘3.3’
define the version of the stack file. Docker enables different features in each version of the docker-compose file.
Services:
Service holds the information about a set of containers which runs in the docker swarm. In the above example we have created a service called nginx and it holds all the information about nginx service such as image need to be used, the number of replicas need to run, ports publishing for nginx service. In a stack file you can create multiple services there is no limit for that.
Under service you can see image: this defines which docker image needs to run for this service.

below is the example for multiple services in a single stack file, where we have nginx and apache services.

version: '3.3'
services:
  nginx:
    image: nginx:latest
    ports:
     - 80:80
    volumes:
     - /opt/nginx.conf:/etc/nginx/nginx.conf
    networks:
     - default
    logging:
      driver: json-file
    deploy:
      replicas: 1
  apache:
    image: apache:latest
    ports:
     - 81:80
    logging:
      driver: json-file
    deploy:
      replicas: 1

You can mention the docker image tag, if not specified latest tag will be used for the docker image
ports: this enables container ports to publish with docker host to access externally. You don’t need to worry when you run multiple replicas for the same service because you have facing port binding issue when you run the same docker image with the same port number. Docker swarm handles that. Here the port is bound for the services and swarm does the internal balancing within the number of containers in the service.

    volumes:
     - /opt/nginx.conf:/etc/nginx/nginx.conf

This is volume mapping for docker containers. When you run 10 replicas on 2 nodes joined to swarm, make sure that the file or folder which needs to be mount is created or available on all the nodes. Otherwise, this service won’t run as expected.

    networks:
     - default

While coming to the network, the docker swarm uses an overlay network to communicate with containers on all the nodes. Either you can use the default overlay network or create your own network using
docker network create <network-name> –driver overlay

    deploy:
      replicas: 1

deploy section contains how the deployment needs to happen. In this example, the nginx service is deployed with a single replica of containers. if you need more replica of containers then change the replica number to the desired number.

networks:
  default:
    external: true

This enables the service published port is accessible externally.

Hope you all understood, comment if you have any doubts. Stay tuned for more articles.

 

 

 

 

© 2020, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points

Related posts

Leave a Reply