Sitemap
Rubaiyat Rahim

Rubaiyat Rahim

Docker CE — Services

2 min readAug 16, 2023

--

Notes taken from A Cloud Guru course titled “Docker Certified Associate (DCA)” by Will Boyd.

Docker Service

  • Used to run an application on a Docker Swarm.
  • Specifies a set of one or more replica tasks to be distributed automatically across the nodes in the cluster and executed as containers.

Create Services

# Create a simple service running the nginx image.
docker service create nginx

# Create an nginx service with a specified name, multiple replicas, and a published port.
docker service create --name nginx --replicas 3 -p 8080:80 nginx

# Use a GO template to pass the node hostname to each container as an environment variable.
docker service create --name node-hostname --replicas 3 --env NODE_HOSTNAME="{{.Node.Hostname}}" nginx

# Get the container running on the current machine, and print its environment variables
# to verify that the NODE_HOSTNAME variable is set properly.
docker ps
docker exec <CONTAINER_ID> printenv

View Services

# List the services in the cluster.
docker service ls

# List the tasks for a service.
docker service ps nginx

# Inspect a service.
docker service inspect nginx
docker service inspect --pretty nginx

Update and Delete Services

# Change a service.
docker service update --replicas 2 nginx

# Delete a service.
docker service rm nginx

Get Rubaiyat Rahim’s stories in your inbox

Join Medium for free to get updates from this writer.

Replicated vs Global Services

  • Replicated services run the requested number of replica tasks across the swarm cluster.
  • Global Services run one task on each node in the cluster.
# Replicated service
docker service create --replicas 3 nginx

# Global service
docker service create --mode global nginx

Scaling Services

Scaling Services means changing the number of replica tasks.

# Two different ways to scale a service:
docker service update --replicas 3 nginx
docker service scale nginx=4

# List the tasks for a service.
docker service ps nginx

--

--