Docker Certified Associate Exam Course

Docker Swarm

Swarm Services

Docker services enable you to deploy, scale, and manage containers across a Swarm cluster. With built-in load balancing, health checks, and automatic rescheduling, services take container orchestration to the next level.


From docker run to Swarm Services

On a single Docker host, you might launch a web server like this:

docker run --name webserver httpd

But scaling out across multiple nodes manually requires SSHing into each host:

docker run httpd

Challenges of this approach:

  • You must configure external load balancing.
  • Health monitoring and restarts are manual.
  • Deployment becomes error-prone as the cluster grows.

Swarm services automate all of these tasks:

  • Unified load balancing across nodes
  • Built-in health monitoring and self-healing
  • Declarative desired state and scaling

Creating a Docker Service

A Swarm service represents one or more replicas of the same container. On a manager node, run:

docker service create \
  --name web \
  --replicas 3 \
  --publish 80:80 \
  httpd:alpine
FlagDescriptionExample
--nameAssigns a name to the serviceweb
--replicasNumber of container replicas to run3
--publishMaps a host port to container port80:80
ImageContainer image (with optional tag)httpd:alpine

Default Replicas

If you omit --replicas, Docker defaults to a single replica (1).


How Swarm Orchestration Works

When you issue docker service create, the Swarm manager components collaborate:

  1. API Server
    Accepts the CLI/API request.
  2. Orchestrator
    Calculates desired tasks (one per replica).
  3. Allocator
    Assigns virtual IPs and ports to each task.
  4. Dispatcher
    Sends tasks to available worker nodes.
  5. Worker Node
    Launches containers and reports status back to the manager.
  6. Rescheduler
    Detects failures and automatically replaces failed tasks.

Swarm Task vs. Container

A task is the Swarm abstraction for running a container and maintaining its desired state.


Managing Services

Use the following commands to inspect and troubleshoot services:

CommandDescription
docker service lsList all services in the Swarm cluster
docker service ps <service>View tasks (containers) for a specific service
docker service inspect <service> --prettyShow detailed service configuration in human-readable form
docker service logs <service>Retrieve aggregated logs from all service replicas
docker service scale <service>=<replicas>Adjust the number of replicas on the fly

Example:

docker service ls
ID            NAME  MODE        REPLICAS  IMAGE         PORTS
3zhe91mns5vz  web   replicated  3/3       httpd:alpine  *:80->80/tcp

Sample logs:

web.1.xxxxxx@worker1 | [mpm_event:notice] AH00489: Apache/2.4.43 configured
web.2.yyyyyy@worker2 | [core:notice] AH00994: Command line: 'httpd -D FOREGROUND'
web.3.zzzzzz@worker3 | 10.0.0.4 - - [24/Apr/2020] "POST /cgi-bin/main.cgi" 400 226

Removing a Service

To tear down a service and its tasks:

docker service rm web

All associated containers will be stopped and removed.



Happy containerizing! See you in the next guide.

Watch Video

Watch video content

Previous
Demo Auto Lock