How to use docker service rollback command to revert service updates

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively manage service updates in a Docker Swarm environment. We will begin by creating a Docker Swarm service, which serves as the foundation for deploying and scaling containerized applications.

Following the service creation, you will perform an update to the service's configuration, simulating a typical deployment scenario. The core focus of this lab is then to demonstrate how to use the docker service rollback command to revert the service back to its previous, stable version. Finally, you will verify the service's configuration after the rollback to confirm the successful restoration.


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/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/ls -.-> lab-555230{{"How to use docker service rollback command to revert service updates"}} docker/ps -.-> lab-555230{{"How to use docker service rollback command to revert service updates"}} docker/inspect -.-> lab-555230{{"How to use docker service rollback command to revert service updates"}} docker/create -.-> lab-555230{{"How to use docker service rollback command to revert service updates"}} docker/pull -.-> lab-555230{{"How to use docker service rollback command to revert service updates"}} end

Create a Docker Swarm service

In this step, we will learn how to create a Docker Swarm service. A Docker Swarm service is a set of tasks that run on a Swarm cluster. Each task is an instance of a container. Services are the central concept of Docker Swarm, allowing you to define how many replicas of a container image should be running and how they should be updated.

Before creating a service, we need to initialize a Docker Swarm. Since we are running on a single VM, we will initialize a single-node Swarm.

First, let's initialize the Swarm:

docker swarm init --advertise-addr 127.0.0.1

You should see output indicating that the Swarm has been initialized and that the current node is now a manager.

Now, let's create a service using the nginx image. We will name the service my-nginx and specify that we want 3 replicas of the nginx container running.

First, pull the nginx image to ensure it's available locally:

docker pull nginx:latest

Now, create the service:

docker service create --name my-nginx --replicas 3 nginx:latest

This command creates a new service named my-nginx with 3 replicas using the nginx:latest image. Docker Swarm will automatically distribute these replicas across the nodes in the Swarm (in this case, just our single node).

To verify that the service has been created and the replicas are running, you can list the services:

docker service ls

You should see my-nginx listed with 3/3 replicas running.

You can also inspect the tasks associated with the service:

docker service ps my-nginx

This command will show you the individual tasks (containers) that make up the my-nginx service, their current state, and the node they are running on. You should see 3 tasks in the Running state.

Update the service to a new configuration

In this step, we will update the my-nginx service to use a different version of the nginx image. This demonstrates how to perform a rolling update of a service in Docker Swarm.

First, let's pull the nginx:1.21 image. This is a specific version of Nginx that we will update our service to.

docker pull nginx:1.21

Now, we will update the my-nginx service to use the nginx:1.21 image. We will also change the number of replicas to 5.

docker service update --image nginx:1.21 --replicas 5 my-nginx

This command tells Docker Swarm to update the my-nginx service. The --image nginx:1.21 flag specifies the new image to use, and the --replicas 5 flag changes the desired number of replicas to 5. Docker Swarm will perform a rolling update, replacing the old containers with new ones running the specified image and adjusting the number of replicas.

You can monitor the progress of the update by listing the service tasks:

docker service ps my-nginx

You will see new tasks being created with the nginx:1.21 image and old tasks being shut down. Eventually, you should see 5 tasks in the Running state, all using the nginx:1.21 image.

You can also check the service details to confirm the update:

docker service inspect my-nginx --pretty

Look for the Image and Replicas fields in the output to confirm that they have been updated to nginx:1.21 and 5 respectively.

Roll back the service to the previous version

In this step, we will roll back the my-nginx service to its previous configuration. Docker Swarm keeps track of previous service configurations, allowing you to easily revert to a known good state if an update introduces issues.

To roll back the service, we use the docker service update command with the --rollback flag.

docker service update --rollback my-nginx

This command instructs Docker Swarm to roll back the my-nginx service to the configuration it had before the last update. In our case, this means reverting to the nginx:latest image and the original number of replicas (which was 3).

You can monitor the rollback process by listing the service tasks:

docker service ps my-nginx

You will see tasks running the nginx:1.21 image being shut down and new tasks running the nginx:latest image being created. The number of tasks will also adjust back to the previous replica count.

Once the rollback is complete, you should see tasks running the nginx:latest image and the number of replicas should be back to 3.

You can also inspect the service details to confirm the rollback:

docker service inspect my-nginx --pretty

Look for the Image and Replicas fields in the output. They should now reflect the configuration before the last update.

Verify the service configuration after rollback

In this step, we will explicitly verify that the my-nginx service has been successfully rolled back to its previous configuration. This involves checking both the image being used by the service and the number of replicas.

First, let's list the services to see the current state:

docker service ls

You should see my-nginx listed, and the REPLICAS column should show 3/3. This indicates that there are 3 desired replicas and 3 are currently running.

Next, let's inspect the service details to confirm the image and replica count.

docker service inspect my-nginx --pretty

In the output, look for the Image field. It should now be nginx:latest. Also, look for the Replicas field, which should be 3.

Finally, let's look at the tasks associated with the service to confirm that the running containers are using the correct image.

docker service ps my-nginx

You should see 3 tasks listed, and the IMAGE column for each task should be nginx:latest. The CURRENT STATE for all tasks should be Running.

By performing these checks, we can be confident that the rollback was successful and the service is running in its desired previous state.

Summary

In this lab, we learned how to manage Docker Swarm services, specifically focusing on creating and updating them. We began by initializing a single-node Docker Swarm and then created a service named my-nginx with three replicas using the nginx:latest image. We verified the service creation and the running tasks using docker service ls and docker service ps.

Following the service creation, we explored how to update the service configuration. This involved changing the image used by the service, demonstrating a common scenario for service updates in a Swarm environment.