How to use docker service update command to modify a service

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively manage and modify Docker services using the docker service update command. You will start by creating a simple service, then explore how to scale it by updating the number of replicas.

Furthermore, you will learn how to perform a rolling restart of your service, add or remove published ports, and understand how to roll back your service to a previous version, providing essential skills for managing the lifecycle of your containerized applications in a Docker Swarm environment.


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/restart("Restart Container") 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-555232{{"How to use docker service update command to modify a service"}} docker/ps -.-> lab-555232{{"How to use docker service update command to modify a service"}} docker/restart -.-> lab-555232{{"How to use docker service update command to modify a service"}} docker/inspect -.-> lab-555232{{"How to use docker service update command to modify a service"}} docker/create -.-> lab-555232{{"How to use docker service update command to modify a service"}} docker/pull -.-> lab-555232{{"How to use docker service update command to modify a service"}} end

Create a simple service

In this step, you will learn how to create a simple Docker service. A service is a group of containers of the same image. Services are useful for scaling your application and ensuring high availability.

First, let's pull the alpine image from Docker Hub. This image is very small and useful for testing.

docker pull alpine

You should see output indicating that the image is being pulled and downloaded.

Now, let's create a service named my-service using the alpine image. We will run a simple command within the container that prints "Hello, LabEx!" and then exits.

docker service create --name my-service alpine echo "Hello, LabEx!"

This command creates a new service. The --name my-service flag gives the service a name. alpine is the image to use, and echo "Hello, LabEx!" is the command to run inside the container.

You should see output similar to this, indicating the service ID:

<service_id>

To verify that the service was created successfully, you can list the services:

docker service ls

You should see my-service listed in the output.

Update service replicas

In this step, you will learn how to scale a Docker service by updating the number of replicas. Replicas are identical copies of your service's containers. Increasing the number of replicas allows your service to handle more load and provides higher availability.

Currently, our my-service has only one replica. You can see this in the output of docker service ls under the "REPLICAS" column.

Let's scale the service to 3 replicas. We can do this using the docker service update command with the --replicas flag.

docker service update --replicas 3 my-service

This command tells Docker Swarm to update the my-service to have 3 replicas. Docker Swarm will automatically create the additional containers needed to reach the desired replica count.

You should see output indicating that the service was updated.

To verify that the service has been scaled, list the services again:

docker service ls

Look at the "REPLICAS" column for my-service. It should now show 3/3, indicating that 3 replicas are desired and 3 are currently running.

You can also inspect the tasks associated with the service to see the individual containers:

docker service ps my-service

This command will show you the status of each replica (task) of the service. You should see three tasks listed, likely with a state of "Running".

Perform a rolling restart of the service

In this step, you will learn how to perform a rolling restart of a Docker service. A rolling restart updates the service's tasks one by one, ensuring that the service remains available during the update process. This is crucial for maintaining application uptime.

To trigger a rolling restart, we can use the docker service update command with the --force flag. This flag forces a new update even if the service configuration hasn't changed.

docker service update --force my-service

This command will initiate a rolling restart of the my-service. Docker Swarm will stop and start each replica of the service sequentially.

You should see output indicating that the service was updated.

To observe the rolling restart in progress, you can continuously monitor the service tasks:

docker service ps my-service

Run this command multiple times. You will see the "CURRENT STATE" of the tasks change from "Running" to "Shutdown" and then back to "Running" as each replica is restarted. This process happens one replica at a time, demonstrating the rolling nature of the restart.

Once all tasks have been restarted, the output of docker service ps my-service will show all tasks in the "Running" state with updated timestamps under the "UPDATED" column.

Add or remove a published port

In this step, you will learn how to add or remove a published port for a Docker service. Publishing a port makes a port inside the container accessible from outside the Docker Swarm cluster. This is essential for exposing your application to users or other services.

Our current my-service doesn't have any published ports because it just runs a simple echo command and exits. To demonstrate publishing ports, let's create a new service that runs a simple web server. We will use the nginx image for this.

First, pull the nginx image:

docker pull nginx

Now, let's create a new service named web-service and publish port 80 of the container to port 8080 on the host.

docker service create --name web-service --publish 8080:80 nginx

This command creates a service named web-service using the nginx image. The --publish 8080:80 flag maps port 80 inside the container to port 8080 on the host.

You should see output indicating the service ID.

To verify that the port is published, you can inspect the service:

docker service inspect web-service

Look for the EndpointSpec section in the output. You should see an entry under Ports that shows the published port mapping (e.g., PublishedPort: 8080, TargetPort: 80).

Now, let's remove the published port from the web-service. We can do this using the docker service update command with the --publish-rm flag, specifying the target port inside the container.

docker service update --publish-rm 80 web-service

This command removes the port mapping for port 80 inside the container.

You should see output indicating that the service was updated.

To verify that the port has been removed, inspect the service again:

docker service inspect web-service

The EndpointSpec section should no longer show the port mapping you just removed.

Roll back the service to a previous version

In this step, you will learn how to roll back a Docker service to a previous version. Rolling back is essential when a new service update introduces issues. Docker Swarm keeps track of previous service configurations, allowing you to quickly revert to a stable state.

First, let's simulate an update that we might want to roll back from. We'll update the web-service to use a different, hypothetical image version (though we won't actually pull it, the command structure is what matters for rollback).

docker service update --image nginx:1.20.0 web-service

This command attempts to update the web-service to use the nginx:1.20.0 image. In a real scenario, this might be a new version with a bug.

Now, let's say we discover an issue with this update and want to roll back to the previous version (which was using the default nginx image). We can use the docker service rollback command.

docker service rollback web-service

This command tells Docker Swarm to revert the web-service to its previous configuration. Docker Swarm will stop the tasks running the new image and start tasks using the previous image version.

You should see output indicating that the service is being rolled back.

To verify that the rollback was successful, you can inspect the service again:

docker service inspect web-service

Look at the Image field in the output. It should now show the original nginx image (without the :1.20.0 tag, assuming you pulled the latest default image initially).

You can also check the service tasks to see the containers running the older image:

docker service ps web-service

The tasks should now be running the original image.

Summary

In this lab, you learned how to manage Docker services using the docker service update command. You began by creating a simple service using the alpine image and a basic echo command.

Subsequently, you explored key service update operations. You scaled the service by modifying the number of replicas, performed a rolling restart to update service tasks without downtime, and learned how to add or remove published ports to expose the service externally. Finally, you practiced rolling back the service to a previous version, demonstrating how to revert changes if an update causes issues. These steps provided practical experience in managing the lifecycle and configuration of Docker services.