List all stacks
In this step, you will learn how to list all Docker stacks running on your system. A Docker stack is a collection of services that are deployed together. Before we can list stacks, we need to install Docker Compose, which is used to define and run multi-container Docker applications.
First, let's install Docker Compose. We will download the Docker Compose binary and make it executable.
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
The first command downloads the Docker Compose binary from the official GitHub repository. $(uname -s)
and $(uname -m)
are used to get the operating system and architecture of your machine, ensuring you download the correct binary. The second command makes the downloaded file executable.
Now that Docker Compose is installed, let's verify the installation by checking the version.
docker-compose --version
You should see the installed Docker Compose version in the output.
To demonstrate listing stacks, we need to deploy a simple stack. We will create a docker-compose.yml
file that defines a simple web service.
nano ~/project/docker-compose.yml
Paste the following content into the docker-compose.yml
file:
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "80:80"
This docker-compose.yml
file defines a single service named web
that uses the nginx:latest
image and maps port 80 on the host to port 80 in the container.
Now, let's deploy this stack using the docker stack deploy
command. We will name our stack mywebstack
.
docker stack deploy -c ~/project/docker-compose.yml mywebstack
The -c
flag specifies the Compose file to use, and mywebstack
is the name we are giving to our stack. You will see output indicating the services are being created and deployed.
After deploying the stack, you can list all running stacks using the docker stack ls
command.
docker stack ls
This command will display a list of all deployed stacks, including the name of the stack, the number of services, and the desired and current number of replicas for each service. You should see mywebstack
listed in the output.