Skip to main content

Deploying Services in Swarm

1. Creating Services

Option Values Description
docker service create docker service create [OPTIONS] IMAGE [COMMAND] [ARG...] Creates a new service from an image
--name service-name Name of the service to be created
--replicas 3 Number of tasks (containers) to run
--port 80:80 Map host port to container port (host:container)
--network my-network Network to connect the service to
--env ENV=value Set environment variables in the service

Example: Creating a Web Service

docker service create --name web-server --replicas 3 --port 80:80 nginx

Example: Creating a Service with Environment Variables

docker service create --name api-server --env NODE_ENV=production --replicas 2 node:18 app.js

2. Listing Services

Command Description Options
docker service ls List all services in the swarm --format to format output, --filter to filter results
docker service ls --format json Display services in JSON format Useful for scripting and parsing
docker service ls --filter name=service_name Filter services by name Matches services with the specified name prefix

Example: Listing all services in a swarm

docker service ls

Example: Listing services with JSON format

docker service ls --format json

3. Inspecting Services

Property Description Example
docker service inspect <service-name> Displays detailed information about a service docker service inspect my-web-service
docker service inspect --format Formats the output using a Go template docker service inspect --format "{{.ID}}" my-web-service

Example: Inspecting a Service

docker service inspect my-web-service

Example: Formatting Service Inspection Output

docker service inspect --format "{{.ID}}: {{.Spec.Name}}" my-web-service

4. Listing Service Tasks

Command Description Options Example
docker service tasks List tasks for a service --filter, --format, --no-trunc docker service tasks myservice

Example: Listing tasks for a service

docker service tasks myservice

Example: Filtering tasks by status

docker service tasks --filter desired-state=running myservice

Example: Formatting task output as JSON

docker service tasks --format &#34;{{.ID}} - {{.Status.State}}&#34; myservice

5. Viewing Service Logs

Command Description Options
docker service logs View logs for a service --details, --follow, --since, --tail, --timestamps

Example: View service logs

docker service logs myservice

Example: Follow service logs

docker service logs --follow myservice

Example: View logs since a specific time

docker service logs --since 1h myservice

Example: View last 100 log lines

docker service logs --tail 100 myservice

6. Configuring Replicas

Option Values Description
--replicas integer Number of service replicas to run
--replicas-max integer Maximum number of replicas to scale up to

Example: Setting Replica Count

docker service create --name web-server --replicas 3 nginx

Example: Scaling Replicas

docker service scale web-server=5
Note: The --replicas flag sets the initial number of service instances (replicas) to run. These replicas are distributed across the swarm nodes.
Warning: If a service is configured with --replicas 0, it will be stopped but not removed. Use docker service rm to fully remove it.

7. Scaling Services

Command Description Example
docker service scale Scales the number of tasks (containers) for a service docker service scale myapp_web=5

Example: Scaling a Service Up

docker service scale frontend=10

Example: Scaling Down to Zero

docker service scale frontend=0
Note: Scaling a service to zero stops all tasks but keeps the service definition.

Scaling Service Workflow

  1. Use docker service scale <service>=<replicas>
  2. Replicas can be increased or decreased
  3. Service tasks are automatically distributed across nodes

8. Updating Services

Option Values Description
docker service update --image Update the service image to a new version
docker service update --replicas Change the number of running tasks
docker service update --env Update environment variables for the service
docker service update --limit Update resource limits (CPU, memory)
docker service update --restart-condition Update restart policy for tasks

Example: Update Service Image

docker service update --image nginx:1.25 my_web_service

Example: Scale Service Replicas

docker service update --replicas 5 my_web_service
Note: Updates are applied gradually to maintain service availability.
Warning: Ensure new image versions are compatible with existing configurations.

9. Setting Update Parallelism

Option Values Description
--update-parallelism 0 or 1 Number of tasks to update simultaneously. Set to 0 for unlimited parallelism.
--update-delay 0s or time duration Delay between updates of tasks. Default is 0s.
--update-order stop-first or start-first Determines the order in which tasks are updated. Default is stop-first.

Example: Update Parallelism in Service Deployment

docker service create \
  --name myapp \
  --update-parallelism 2 \
  --update-delay 10s \
  --update-order start-first \
  nginx:latest
Note: Setting --update-parallelism to 0 means all tasks will be updated simultaneously, which can lead to downtime if not handled carefully.

10. Rolling Back Services

Option Values Description
docker service rollback service-name Rolls back a service to its previous version

Example: Rolling Back a Service

docker service rollback my-web-app
Note: The rollback operation reverts the service to the last known good configuration. If no previous version exists, it will fail.

11. Configuring Placement Constraints

Constraint Description Example
node.labels Matches nodes with specific labels node.labels.environment == production
node.id Matches nodes by ID node.id == abc123
node.hostname Matches nodes by hostname node.hostname != worker1
node.role Matches nodes by role (manager/worker) node.role == manager
node.platform.os Matches nodes by OS type node.platform.os == linux
node.platform.arch Matches nodes by architecture node.platform.arch == x86_64

Example: Deploying Service with Placement Constraints

docker service create \
  --name web-app \
  --constraint 'node.labels.environment == production' \
  --constraint 'node.platform.os == linux' \
  nginx:latest
Note: Placement constraints are evaluated at service creation time and can be updated later using docker service update.

12. Removing Services

Option Values Description
docker service rm <service_name> Removes one or more services by name
docker service rm --force <service_name> Force removal even if service is running

Example: Remove a Service

docker service rm my-web-app

Example: Force Remove a Service

docker service rm --force my-web-app
Note: Removing a service does not remove the underlying images or data volumes unless explicitly configured to do so.

Was this article helpful?