Home > Operation and Maintenance > Docker > How to limit the memory size of docker containers

How to limit the memory size of docker containers

PHPz
Release: 2023-04-17 17:28:47
Original
9464 people have browsed it

Docker is one of the most popular containerization technologies today, which allows applications to run in self-contained containers. An important feature of Docker is that it can manage and limit the CPU and memory usage of containers, which is crucial for maintaining high availability and stability of applications. In this article, we will focus on how to limit the memory size of Docker containers.

1. Understand Docker’s memory management

In Docker, each container has its own namespace and resource restrictions. A container with a memory limit can be started using the following command:

docker run -it --memory 512m --name example_container ubuntu:latest
Copy after login

In this example, we create a container named example_container and set its memory limit to 512MB. The command also specifies the base image used by the container - ubuntu:latest. Note that the "-it" symbol is a way of telling Docker to attach the container's stdin and stdout to that terminal session.

The above command can verify the current memory usage of the container through the following command:

docker stats example_container
Copy after login
Copy after login

With this command, you can view the CPU usage, memory usage, network transmission rate, etc. of the container in real time .

2. Methods to limit container memory

In addition to command line options, when the container is running, you can also use the following methods to limit the memory usage of the container:

1. Use the "docker update" command

You can use the "docker update" command to modify the memory limit of a running container. The following command can modify the memory limit of example_container to 1GB:

docker update --memory 1g example_container
Copy after login

2. Using docker-compose

In the Docker Compose environment, you can use the "mem_limit" keyword to set the memory limit of the container . The following is an example of a container configuration file, where the container is named "db" and its memory limit is set to 512MB:

version: '3'
services:
  db:
    image: mongo:4.4
    mem_limit: 512m
Copy after login

In this example, we used the mongo:4.4 image as the container's base image and name it "db". In the "mem_limit" keyword, we set the memory limit of this container to 512MB.

3. How to test the container memory limit

In order to verify whether the container's memory limit is in effect, we can run some memory-intensive tasks in the container, for example, the benchmark tool stress-ng's " matrix" test. Here is an example where we run the "matrix" test to take up more memory:

docker exec example_container stress-ng --matrix 1 -t 10s
Copy after login

In this example, we use the "docker exec" command to run stress-ng's "matrix" in the running container example_container "Test, duration is 10 seconds.

During the test, we can use the following command to observe the memory usage of the container:

docker stats example_container
Copy after login
Copy after login

Under normal circumstances, stress-ng should occupy a lot of memory for the specified test duration. If the container successfully uses only the specified memory limit, then this test should result in an out-of-memory condition.

Summary

Through this article, we learned how to limit the memory usage of Docker containers. Docker provides several ways to control the memory usage of containers. In order to keep containers stable and highly available, it is necessary to limit the memory usage of containers. Through experiments and benchmarks, we can verify that containers correctly adhere to specified memory limits.

The above is the detailed content of How to limit the memory size of docker containers. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template