Feedback

Chat Icon

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Deploying Applications on Docker Swarm: A Practical Guide
90%

Docker Swarm Resource Management

As with standalone containers, we can configure services with resource reservations and limits. This is useful if you want to control the amount of resources that a service can use.

Let's start with this example:

# remove old services
docker service rm $(docker service ls -q)

# create the service
docker service create \
  --name webserver \
  --replicas 3 \
  --reserve-memory 100m \
  --limit-memory 300m \
  --reserve-cpu 0.5 \
  --limit-cpu 1 \
  -p 80:80 \
  nginx

As you can see, we used the following options:

  • --reserve-memory: This option is used to reserve memory for the service. In our example, we reserved 100MiB of memory for the service (MiB stands for Mebibyte).
  • --limit-memory: This option is used to limit the memory that the service can use. In our example, we limited the memory to 300MiB.
  • --reserve-cpu: This option is used to reserve CPU for the service. In our example, we reserved 0.5 CPU for the service.
  • --limit-cpu: This option is used to limit the CPU that the service can use. In our example, we limited the CPU to 1.

You can see how Docker converts the values to the format it uses internally with the following command:

docker service inspect \
  --format "{{ json .Spec.TaskTemplate.Resources }}" \
  webserver | jq

You should see this:

{

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Enroll now to unlock all content and receive all future updates for free.

Unlock now  $31.99$25.59

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More