Understanding How Docker Swarm Works
Swarm Services and Tasks
In this section, we are going to create a service that runs a web server. We will use the nginx image to create the service.
To create the service, run the following command on the manager node:
docker service create --name webserver -p 80:80 nginx:1.29.5
This creates a service called webserver that runs the nginx image. Swarm's routing mesh publishes port 80 on every node and forwards traffic to the running tasks. In other words, launching the service on the manager node will create a container on one of the VMs (either the manager or one of the workers) and make it accessible on port 80 of all the nodes in the cluster. The container may be killed and restarted on the same or another node, but it will always be accessible on port 80 of all the nodes in the cluster.
To see the services running on the cluster, run the following command on the manager node:
docker service ls
To see the tasks running on the cluster, run the following command on the manager node:
docker service ps webserver
A task is a running container that is part of a service.
Painless Docker - 2nd Edition
A Comprehensive Guide to Mastering Docker and its EcosystemEnroll now to unlock all content and receive all future updates for free.
Hurry! This limited time offer ends in:
To redeem this offer, copy the coupon code below and apply it at checkout:
