How to use docker stack rm command to remove stacks

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker stack rm command to remove Docker stacks deployed on a Docker Swarm environment. You will begin by preparing a single-node Docker Swarm, initializing it and verifying its status.

Following the environment setup, you will deploy a sample stack using a Docker Compose file to understand the deployment process. The core of the lab focuses on removing stacks: you will first practice removing a single deployed stack, and then deploy multiple stacks to learn how to remove several stacks simultaneously using the docker stack rm command. This hands-on experience will equip you with the skills to manage and clean up your Docker Swarm deployments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/SystemManagementGroup(["System Management"]) docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/SystemManagementGroup -.-> docker/info("Display System-Wide Information") docker/SystemManagementGroup -.-> docker/version("Show Docker Version") subgraph Lab Skills docker/ls -.-> lab-555237{{"How to use docker stack rm command to remove stacks"}} docker/ps -.-> lab-555237{{"How to use docker stack rm command to remove stacks"}} docker/rm -.-> lab-555237{{"How to use docker stack rm command to remove stacks"}} docker/pull -.-> lab-555237{{"How to use docker stack rm command to remove stacks"}} docker/info -.-> lab-555237{{"How to use docker stack rm command to remove stacks"}} docker/version -.-> lab-555237{{"How to use docker stack rm command to remove stacks"}} end

Prepare a Docker Swarm environment

In this step, you will prepare your environment for deploying Docker stacks by initializing a Docker Swarm. Docker Swarm is a native clustering and orchestration solution for Docker. It allows you to create and manage a swarm of Docker nodes.

First, let's check if Docker is running and accessible. You can do this by running a simple Docker command.

docker version

You should see output showing the Docker client and server versions. This confirms that Docker is installed and running.

Next, we need to initialize the Docker Swarm. Since this is a single-node environment, we will initialize the swarm on the current machine.

docker swarm init

This command initializes the swarm and makes the current node a manager node. The output will provide information about joining other nodes to the swarm, but for this lab, we will only use a single node.

To confirm that the swarm has been initialized successfully, you can check the swarm status.

docker info | grep Swarm

The output should show Swarm: active, indicating that the swarm is active on this node.

Finally, let's verify that the current node is part of the swarm and is a manager.

docker node ls

You should see a list of nodes in the swarm, with the current node listed and its status as Ready and Leader.

Deploy a sample stack

In this step, you will learn how to deploy a sample application as a stack on your Docker Swarm. A stack is a group of related services that share dependencies and can be managed together. We will use a simple example involving an Nginx web server.

Before deploying the stack, we need to create a Docker Compose file that defines the services in our stack. Docker Compose is a tool for defining and running multi-container Docker applications. Although Docker Compose is not pre-installed, the docker stack deploy command can read Compose files.

First, let's create a directory for our stack definition. Navigate to your home directory and create a new directory named my-nginx-stack.

cd ~/project
mkdir my-nginx-stack
cd my-nginx-stack

Now, create a file named docker-compose.yml inside the my-nginx-stack directory using the nano editor.

nano docker-compose.yml

Paste the following content into the docker-compose.yml file:

version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure

This docker-compose.yml file defines a single service named web that uses the nginx:latest image. It maps port 80 on the host to port 80 in the container and specifies that there should be one replica of this service. The restart_policy ensures that the service will be restarted if it fails.

Save the file by pressing Ctrl + X, then Y, and Enter.

Before deploying, let's pull the nginx:latest image to ensure it's available locally.

docker pull nginx:latest

Now, deploy the stack using the docker stack deploy command. We will name our stack nginx_stack.

docker stack deploy -c docker-compose.yml nginx_stack

This command reads the docker-compose.yml file and creates the services defined within it as part of the nginx_stack.

To verify that the stack has been deployed and the service is running, you can list the services in the stack.

docker stack services nginx_stack

You should see output showing the nginx_stack_web service with 1/1 replicas running.

You can also check the running containers to see the Nginx container.

docker ps

You should see a container running the nginx:latest image, associated with the nginx_stack_web service.

Finally, you can access the Nginx web server by opening a web browser and navigating to the IP address of your LabEx VM. You should see the default Nginx welcome page.

Remove a single stack

In this step, you will learn how to remove a deployed stack from your Docker Swarm. Removing a stack will stop and remove all the services and containers associated with that stack.

In the previous step, we deployed a stack named nginx_stack. To remove this stack, we use the docker stack rm command followed by the stack name.

docker stack rm nginx_stack

This command will initiate the removal process. Docker Swarm will stop and remove the service and its associated container.

You should see output indicating that the stack and its services are being removed.

To verify that the stack has been removed, you can list the deployed stacks again.

docker stack ls

The nginx_stack should no longer appear in the list of deployed stacks.

You can also check the running services to confirm that the nginx_stack_web service is no longer running.

docker stack services nginx_stack

This command should return an error or no output, indicating that the stack and its services are gone.

Finally, you can check the running containers to ensure that the Nginx container associated with the stack has been removed.

docker ps

The container running the nginx:latest image for the nginx_stack_web service should no longer be listed.

Deploy multiple sample stacks

In this step, you will learn how to deploy multiple stacks on your Docker Swarm. This demonstrates how you can manage different applications or services independently within the same swarm.

We will deploy two simple stacks: one using Nginx and another using Apache HTTP Server.

First, let's create a directory for the Apache stack definition. Navigate back to the ~/project directory and create a new directory named my-apache-stack.

cd ~/project
mkdir my-apache-stack
cd my-apache-stack

Now, create a file named docker-compose.yml inside the my-apache-stack directory using the nano editor.

nano docker-compose.yml

Paste the following content into the docker-compose.yml file:

version: "3.8"
services:
  web:
    image: httpd:latest
    ports:
      - "81:80"
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure

This docker-compose.yml file defines a service named web that uses the httpd:latest image. It maps port 81 on the host to port 80 in the container to avoid port conflicts with the Nginx stack we will deploy later. It also specifies one replica and a restart policy.

Save the file by pressing Ctrl + X, then Y, and Enter.

Before deploying, let's pull the httpd:latest image.

docker pull httpd:latest

Now, deploy the Apache stack using the docker stack deploy command. We will name this stack apache_stack.

docker stack deploy -c docker-compose.yml apache_stack

This command deploys the Apache stack.

Next, let's go back to the my-nginx-stack directory to deploy the Nginx stack again.

cd ~/project/my-nginx-stack

Deploy the Nginx stack using the docker stack deploy command. We will name this stack nginx_stack.

docker stack deploy -c docker-compose.yml nginx_stack

Now, let's verify that both stacks have been deployed. List the deployed stacks.

docker stack ls

You should see both apache_stack and nginx_stack listed.

You can also check the services for each stack individually.

docker stack services apache_stack
docker stack services nginx_stack

You should see the apache_stack_web service and the nginx_stack_web service, both running with 1/1 replicas.

You can access the Apache web server by opening a web browser and navigating to the IP address of your LabEx VM on port 81. You should see the default Apache welcome page. The Nginx web server should still be accessible on port 80.

Remove multiple stacks

In this final step, you will learn how to remove multiple deployed stacks from your Docker Swarm. This is similar to removing a single stack, but you will repeat the process for each stack you want to remove.

In the previous step, we deployed two stacks: apache_stack and nginx_stack. We will now remove both of them.

First, let's remove the apache_stack. Use the docker stack rm command followed by the stack name.

docker stack rm apache_stack

You should see output indicating that the apache_stack and its services are being removed.

Next, let's remove the nginx_stack. Use the docker stack rm command followed by the stack name.

docker stack rm nginx_stack

You should see output indicating that the nginx_stack and its services are being removed.

To verify that both stacks have been removed, list the deployed stacks again.

docker stack ls

Neither apache_stack nor nginx_stack should appear in the list of deployed stacks.

You can also check the running containers to ensure that the containers associated with both stacks have been removed.

docker ps

There should be no containers running the httpd:latest or nginx:latest images that were part of these stacks.

This concludes the lab on deploying and removing Docker stacks. You have successfully initialized a Docker Swarm, deployed and removed single and multiple stacks.

Summary

In this lab, you learned how to prepare a Docker Swarm environment by initializing a single-node swarm and verifying its status. You then practiced deploying a sample application as a stack using a Docker Compose file. Finally, you mastered the use of the docker stack rm command to remove both single and multiple deployed stacks from your Docker Swarm, demonstrating the process of cleaning up resources after deployment.