Overview of Docker stack | How Its works and its advantages

 Overview of Docker stack |  How Its works and its advantages
Written by Nilima PaulFebruary 17, 2022
9 min read
Nilima Paul

Technology Security Analyst

How stack works in Docker and its advantages will be discussed in this blog post.

Introduction to Docker Stack:

The 'docker stack' is a Docker order to oversee Docker stacks. We can utilize this order to convey another stack or update a current one, list stacks, list the undertakings in the stack, eliminate at least one stack, and rundown the administrations in the stack. We should empower swarm mode to run this order as we can convey stacks in Docker swarm mode and it is incorporated with the Docker motor so we don't need to introduce any extra bundles, just we want to empower it as it is incapacitated naturally. It is just upheld on the Docker CLI or daemon API rendition 1.25 or above.

How Stack works in Docker:

We need to pass a 'docker-compose' yam record as a contention to the 'docker stack convey' order utilizing the '- make document' or '- ca choice'. The 'docker-compose.YAML' document contains administrations, volumes, organizations, and so forth that are expected to begin an out and out application. They make record rendition 3.0 or more are upheld by conveying order. When we send the stack, administrations referenced in the form record are got made and imitations are circulated to various specialist hubs. The Docker daemon adds a stack name before administration or volume or organization names.

The Docker pictures referenced in the made record are either accessible on any Docker vault, it very well may be private or public library or accessible on all hubs in that multitude, if not compartment creation will come up short.


Let’s create a simple python application and deploy it in a Docker swarm.

1. First of all, create a local Docker register running as a service using the below command :

docker service create --name my-registry -p 5000:5000  registry:2

Note: We need to initialize the Docker swarm (if not initialized already) using the ‘docker swarm init' command before running the above command.

2. Now, let’s verify that the service is running fine and the registry is accessible as shown below :

docker service ls

Explanation: We can see that the service is replicated and able to access the localhost successfully.

3. Let’s work on the application now, we are going to create a python application with the code mentioned below, however, we can use any application : –



from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
def hello():
count = redis.incr('hits')
return 'Hi, You have visited {} times\n'.format(count)
if __name__ == "__main__":
app.run(host="", port=8000, debug=True)

4. We have used Flask and Redis in our python application so we need those packages to run the application.

5. Here is the snippet of Dockerfile to build a Docker image : –


FROM python:3.10.0a1-alpine
ADD. /test-app
WORKDIR /test-app
RUN pip install -r requirements.txt
CMD ["python", "test-app.py"]

6. Let’s build the Docker image now and push it to the local registry as shown below: –

docker build –t localhost:5000/test-app.
docker push localhost:5000/test-app

7. Let’s create the ‘docker-compose.yml’ file that has two services, the first one is ‘web’ and the second one is ‘Redis’. The first service has been exposed to port 8000 externally.


version: '3'
image: localhost:5000/test-app
- "8000:8000"
image: Redis:alpine

8. Finally, deploy our first Docker stack using the above ‘docker-compose.yml’ file and named it ‘my-first-stack’ as shown below: –

docker stack deploy --compose-file docker-compose.yml my-first-stack

In the above example, we can see that the stack name has been added to services and networks.

9. Let’s check if it is deployed successfully using the ‘docker stack ls’ command as below: –

docker stack ls

In the above example, we can see that the ‘my-first-stack’ has 2 services and the orchestrator is Swarm as we have not specified any other orchestrator while deploying the stack.

10. We can use the below command to know more about services running under this stack : –

docker stack services <stack_name>
docker stack services my-first-stack

Using the above command, we will see that 1 reproduction is running out of 1 for both administrations. Assuming there is 0 reproduction showing which implies that the assistance isn't conveyed effectively. Actulookthe compartments and their logs to investigate.

11. To check the status of the containers, we have to use ‘docker stack ps <stack_name>’ as shown below: –

docker stack ps my-first-stack

In the above example, we will see that both containers are running, we will see the error in the error column if any occurs or use the ‘docker logs <container_ID>’ to know more about the error.

12. If there is any requirement to destroy the stack, we can do that by running ‘docker stack rm <stack_name>’ as shown below: –

docker stack rm my-first-stack

In the above snapshot, we will see that it removes all its services and network whatever is created while deploying the stack.

  1. Docker stack makes it simpler to deal with all parts of an application.
  2. We can send an exceptionally accessible application utilizing the Docker stack as it works in swarm mode.
  3. We can utilize various orchestrators like Kubernetes too.

We use 'docker stack' more often than not while sending applications underway to make it exceptionally accessible. It just works in Swarm mode so we should need to actuate or introduce swarm mode.

Docker Stack
Docker secrets
Was this blog helpful?
You must be Logged in to comment
Code Block

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance