Resource Control

Tutorial 2 of 4

Tutorial: Resource Control in Docker

1. Introduction

Tutorial Goal

This tutorial aims to help you understand how resource control is managed in Docker. We will explore methods to limit and assign the amount of memory, CPU, and disk resources that a Docker container can use.

What You Will Learn

By the end of this tutorial, you will be able to:

  • Understand Docker resource controls
  • Set memory and CPU limits for containers
  • Manage disk usage in Docker

Prerequisites

To follow along with this tutorial, you will need:

  • Basic knowledge of Docker
  • Docker installed on your computer

2. Step-by-Step Guide

Docker Resource Controls

Docker allows you to control the resources a container can use through specific flags at the time of container creation. These flags include:

  • --memory (or -m): Memory limit
  • --cpu-shares: CPU usage
  • --disk-quota: Disk usage

Memory Control

To limit the memory that a Docker container can use, use the --memory flag followed by the amount of memory. For example, to limit a container to use a maximum of 100mb of memory, you can use the command:

docker run -it --memory=100m ubuntu

CPU Control

To control the CPU resources, Docker uses a relative weight system with --cpu-shares flag. For example, if two containers have the same CPU share value, they get the same amount of CPU time. If one container has a value of 1024 and another has a value of 512, the first container gets twice as much CPU time than the second.

docker run -it --cpu-shares=1024 ubuntu

Disk Usage

Docker does not directly provide a flag to limit disk usage. However, it can be controlled indirectly by limiting the size of Docker containers.

3. Code Examples

Example 1: Limiting Memory Usage

In this example, we are going to limit a Docker container's memory to 100m.

# Run a Docker container with memory limited to 100mb
docker run -it --memory=100m ubuntu

Example 2: Controlling CPU Usage

In this example, we will set the CPU share of a Docker container to 512.

# Run a Docker container with a CPU share of 512
docker run -it --cpu-shares=512 ubuntu

4. Summary

In this tutorial, we have learned how to control the resources used by Docker containers. We have learned how to limit the memory usage and control the CPU usage of a Docker container. We have also discussed how Docker does not directly provide a way to limit disk usage, but it can be controlled indirectly.

For further learning, you can explore other Docker features like networking, data management, and Docker Compose.

5. Practice Exercises

Exercise 1:

Run a Docker container that has its memory limited to 200m and its CPU share set to 1024.

Solution:

docker run -it --memory=200m --cpu-shares=1024 ubuntu

Exercise 2:

Find out how much CPU time your Docker container is actually using.

Solution:

You can use the docker stats command to get real-time statistics about your Docker containers.

docker stats <container_id>

Remember, practice is key in mastering Docker. Try to run different Docker containers with different resource controls and observe the results.