- [Instructor] With Docker Swarm, you can create services,
which define the desired state for your application.
That service can contain multiple tasks
and those tasks execute containers across the cluster.
But wait, as they say, there's more.
We have reached the top.
We're heading higher in the hierarchy here
and now, we're talking about stacks.
So stacks are the proverbial king of the hill
in the Docker Swarm cluster when it comes to
deploying your applications and running them at scale.
Stacks are used to define complete multi-tiered,
highly available, highly scalable applications
to a swarm cluster.
So let's take a look at a diagram to help you understand
what stacks are and how they work
in the Docker Swarm infrastructure.
So we start off with a web service.
And that web service as we said, can contain multiple tasks,
each which run containers across the Docker Swarm cluster.
Let's say that we're deploying a complex,
multi-tiered application.
We want it to be highly available and highly scalable
across the cluster.
This complex, multi-tier application is going to have more
than just the web service tier.
It's also going to have a database service
and a reporting service, each made up of multiple tasks
running multiple containers.
We can define all of this
inside what's called a Docker stack.
The Docker stack defines these multiple services,
each with multiple containers running inside,
how all the networking services
and storage services will be exposed,
and how they're all related within the stack.
Once we define that stack, Docker Swarm will ensure
that these multiple, interrelated services
remain highly available, and their desired state
that we've defined is constantly maintained.
Let's look at it a different way.
If we start off with a container,
this container could contain a web server.
And then from there, to scale our application,
we need multiple containers running that same web server.
We define those as a service.
Let's call it the web service.
And then from there, this multi-tiered application
needs more services, like we just talked about.
The database service and the reporting service, for example.
And then those multiple services running multiple containers
are defined as a Docker stack with the stack file.
So what is a stack file exactly?
Well, it's a text file that's used to define
a complete, multi-tiered, highly available, highly scalable
application to a swarm cluster.
That text file is written in YAML format, as it's called,
and it defines the stack.
Stack files are portable from desktop to swarm cluster.
Now, let's go to our Docker Swarm cluster
and I'll give you a demonstration
of how to use a stack file.
On this Docker node, I've already created
a Docker stack file
called docker-stack.yml.
If we cat out that stack file,
you can see how the stack file is laid out.
First off, you have the services that are defined.
In this case, there's only one service defined.
Most stack files would have multiple services,
as we saw back in the diagram.
In this case, the web service is going to pull an image
from this repository, which I've already created.
And we're going to run five replicas of that image
as a container to maintain the desired state
of this service.
You could also control limits around CPU and memory.
You can define the exposed networks and storage as well.
This is a simple example of a Docker stack YAML file
that was taken from the Docker documentation.
I'll show you that Docker documentation and the samples
at the end of this video.
But first, let's deploy our Docker stack.
We're going to use the command docker stack
to deploy the Docker stack file,
specifically the docker stack deploy command.
After that, we need to specify the compose file
that we're going to use.
In this case, it's docker-stack.yml
and then the name of the stack that you want to deploy.
Its edit created our new service
and then our stack has been deployed.
But let's run some commands to check it out.
So we're going to use the docker stack command
and ls at the end to list out the stacks.
It says that mystack has been deployed
and there is one service running inside that stack.
If we do a
docker stack ps
on mystack,
you can see that there are indeed five different
virtual machines running as part of this service
that was deployed from the Docker stack file.
And again, I want to point out this is just a simple example,
because our stack file just had a single service.
And most stack files would be used to deploy
multiple services, each with multiple replicas
running multiple tasks and multiple containers inside.
Another useful command is docker stack services.
Docker stack services requires that you specify
the name of the stack.
In our case, it's mystack.
And so here, you can see the name of the stack,
the status of the stack that is replicated,
and there are five replicas,
where the image was pulled,
and then the network configuration
for all the replicas in this stack.
For more information on Docker stacks and stack files,
I hope you'll check out the Docker documentation,
which includes the example Docker stack file
that I showed in this video.