docker swarm stack file Explained – Beginner Level4 min read
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.
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.
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.
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.
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.
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 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.
This enables the service published port is accessible externally.
Hope you all understood, comment if you have any doubts. Stay tuned for more articles.