Docker / Managing Docker Containers

Resource Control

This tutorial focuses on resource control in Docker. It explains how to manage the amount of memory, CPU, and disk resources that a Docker container can use.

Tutorial 2 of 4 4 resources in this section

Section overview

4 resources

Covers managing, stopping, and removing Docker containers effectively.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

Image Converter

Convert between different image formats.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help