How to use docker service rm command to remove services

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to manage Docker services by creating and removing them. We will start by creating a simple service based on the alpine image, which will run a ping command.

After creating the service, you will verify its successful creation by listing the active services using the docker service ls command. Finally, you will learn how to remove the service using the docker service rm command and confirm its removal by listing the services again. This hands-on exercise will provide you with practical experience in basic Docker service management.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/ls -.-> lab-555229{{"How to use docker service rm command to remove services"}} docker/rm -.-> lab-555229{{"How to use docker service rm command to remove services"}} docker/create -.-> lab-555229{{"How to use docker service rm command to remove services"}} docker/pull -.-> lab-555229{{"How to use docker service rm command to remove services"}} end

Create a sample service

In this step, we will create a simple Docker service. A Docker service is a group of containers that work together to provide a specific function. We will use the docker service create command to create a service based on the alpine image.

First, let's pull the alpine image. While the environment has Docker installed, it might not have all images pre-pulled.

docker pull alpine

You should see output indicating that the image is being pulled or that it is already available.

Now, we will create a service named my-alpine-service using the alpine image and running the command ping docker.com. This service will consist of a single replica (one container).

docker service create --name my-alpine-service alpine ping docker.com

After running the command, Docker Swarm will create the service and start the container. You will see output confirming the creation of the service, including its ID.

List services to confirm creation

In the previous step, we created a Docker service. Now, we will verify that the service was created successfully by listing the active services.

To list Docker services, we use the docker service ls command. This command will show you a list of all services running in your Docker Swarm.

docker service ls

You should see output similar to this, showing the my-alpine-service we created:

ID             NAME                MODE         REPLICAS   IMAGE          PORTS
[service_id]   my-alpine-service   replicated   1/1        alpine:latest

The output provides information about the service, including its ID, name, mode (replicated or global), the number of replicas (desired/running), and the image it is using. The 1/1 under REPLICAS indicates that one replica is desired and one is currently running.

Remove the sample service

In this step, we will remove the Docker service we created in the previous steps. Removing a service will stop and remove all the containers associated with that service.

To remove a Docker service, we use the docker service rm command followed by the service name or ID. We will use the service name my-alpine-service.

docker service rm my-alpine-service

You should see output confirming that the service has been removed, typically showing the service name or ID that was removed.

my-alpine-service

This command tells Docker Swarm to terminate the service and clean up the associated resources.

List services to confirm removal

In the previous step, we removed the my-alpine-service. Now, we will confirm that the service is no longer listed among the active services.

We will use the docker service ls command again to list the services.

docker service ls

This time, the output should not include my-alpine-service. If there are no other services running, the output might only show the header row.

ID             NAME                MODE         REPLICAS   IMAGE          PORTS

If you see this output or similar output without my-alpine-service, it confirms that the service was successfully removed.

Summary

In this lab, we learned how to manage Docker services using the command line. We started by creating a sample service named my-alpine-service based on the alpine image, demonstrating the use of docker service create to define a service with a specific command and replica count.

We then confirmed the successful creation of the service by listing active services using docker service ls, observing the service's details including its ID, name, and replica status. Finally, we learned how to remove the service using the docker service rm command and verified its removal by listing services again.