Skip to main content
Welcome to this guide on managing Docker services within a Swarm cluster. In this lesson, you’ll learn how to create, update, scale, and monitor Docker services. We’ll also highlight how running multiple containers as part of a service increases resilience against node failures.

Verifying the Swarm Cluster

First, ensure your Swarm cluster is properly configured. In this example, our cluster consists of three nodes: one manager (master) node and two worker nodes. Run the following command on the manager node to list all nodes within the cluster:

Creating an NGINX Service

Next, create an NGINX service. Because the --detach=false flag was not specified, Docker creates the service tasks in the background:
Docker automatically assigns a random name to the service if no name is provided.

Inspecting the Service

After creating the service, list all services to see the assigned name (in this example, “hopeful_jones”):
To inspect the tasks of the service, run:
If the container is still pulling the NGINX image from Docker Hub, its state might temporarily display as “Preparing”:
You can further verify that the container is running by checking with the docker ps command:
Each service receives a random name, and each task is suffixed with an incremental identifier (e.g., “hopeful_jones.1”). The container’s name is derived from the task name along with a unique task ID.

Publishing a Port

Since NGINX serves web content, you might wish to access it via a web browser. To publish a port so that requests on the host are forwarded to the container, update the service as follows. The command below maps port 5000 on the host to port 80 on the NGINX container:
After the update, confirm the published port with:
Now, open a web browser and navigate to the host address at port 5000 to view the NGINX welcome page.

Removing and Scaling the Service

When you’re done with the service, you can remove it by executing:
After removal, the service list will no longer display any active services. To demonstrate scaling, recreate a similar service this time with multiple replicas. This showcases how Docker Swarm distributes containers across available nodes for enhanced availability:
Check the status of the newly created service:
Inspect the tasks to see their distribution across nodes:
Over time, both replicas should be running and accessible. Initially, one replica might start running before the other is fully up.

Draining the Manager Node

By default, manager nodes in Docker Swarm can run service tasks. However, if you want the manager node to focus solely on control-plane activities, you can drain it so that no tasks are scheduled on it. Execute the following command on the manager node:
Draining the manager node causes any tasks running on it to be shut down and redeployed on other nodes. For example, after draining, the service tasks might appear as follows:
After a short period, Docker Swarm automatically migrates tasks away from the drained manager node.

Simulating a Node Failure

To demonstrate Docker Swarm’s self-healing capabilities, simulate a node failure by shutting down one of the worker nodes. On docker-node1, run the following command:
Once the node is shut down, Docker Swarm will automatically redeploy the affected tasks onto the other available nodes—even if this means placing both replicas on a single node. You can check the current status of running containers on the manager node with:
This self-healing feature ensures your service remains available even if individual nodes fail.

Thank you for reading this guide on managing Docker services in a Swarm cluster. We hope you found the walkthrough helpful. Stay tuned for more advanced topics and best practices in managing containerized applications.

Watch Video

Practice Lab