<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CPU Limiting &#8211; CPYNET</title>
	<atom:link href="https://cpynet.com/tag/cpu-limiting/feed/" rel="self" type="application/rss+xml" />
	<link>https://cpynet.com</link>
	<description>NextGen Tech Hub</description>
	<lastBuildDate>Fri, 22 Nov 2024 11:01:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://cpynet.com/wp-content/uploads/2024/11/cropped-favicon-2-32x32.png</url>
	<title>CPU Limiting &#8211; CPYNET</title>
	<link>https://cpynet.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Limit Memory and CPU Usage in Docker</title>
		<link>https://cpynet.com/limit-memory-and-cpu-usage-in-docker/</link>
		
		<dc:creator><![CDATA[Emin Buyuk]]></dc:creator>
		<pubDate>Fri, 22 Nov 2024 11:01:54 +0000</pubDate>
				<category><![CDATA[Docker]]></category>
		<category><![CDATA[CPU Limiting]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[Memory Limiting]]></category>
		<category><![CDATA[Performance Optimization]]></category>
		<category><![CDATA[Resource Management]]></category>
		<guid isPermaLink="false">https://cpynet.com/?p=4003</guid>

					<description><![CDATA[Docker is a vital tool in modern software development. Its lightweight, portable, and flexible nature simplifies the development&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Docker is a vital tool in modern software development. Its lightweight, portable, and flexible nature simplifies the development process significantly. However, managing Docker containers properly is essential to ensure efficient use of system resources. Limiting memory and CPU usage in Docker is critical for both performance and reliability. This blog post explores how to set memory and CPU limits, the tools to achieve this, and best practices for optimizing resource management.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><strong>1. The Importance of Resource Management in Docker</strong></h2>



<p>Docker containers share the same hardware resources as other applications running on your system. If one container consumes excessive resources, it can negatively impact other containers or applications. By setting resource limits, you can:</p>



<ul class="wp-block-list">
<li>Enhance system stability,</li>



<li>Prevent resource consumption imbalances,</li>



<li>Optimize performance.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><strong>2. Commands for Limiting Memory and CPU in Docker</strong></h2>



<p>Docker provides specific commands to limit memory and CPU usage for containers. These limits can be defined via <code>docker run</code> commands or in <code>docker-compose.yml</code> files.</p>



<h3 class="wp-block-heading"><strong>2.1. Memory Limiting</strong></h3>



<p>Memory limits prevent containers from consuming more than the allocated memory.</p>



<ul class="wp-block-list">
<li><code>--memory</code>: Defines the maximum memory limit.</li>



<li><code>--memory-swap</code>: Sets the total memory limit (RAM + swap).</li>
</ul>



<p>Example:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="docker run --memory=512m --memory-swap=1g my-container
" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #50FA7B">docker</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">run</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">--memory=512m</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">--memory-swap=1g</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">my-container</span></span>
<span class="line"></span></code></pre></div>



<p>This command restricts the container to 512 MB of RAM and allows up to 1 GB, including swap memory.</p>



<h4 class="wp-block-heading"><strong>What Is Swap?</strong></h4>



<p>Swap is disk space used as additional memory when physical RAM is insufficient. However, excessive swap usage can significantly impact performance, so configure it carefully.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>2.2. CPU Limiting</strong></h3>



<p>CPU limits control how much processing power a container can use.</p>



<ul class="wp-block-list">
<li><code>--cpus</code>: Sets a CPU usage percentage limit.</li>



<li><code>--cpu-shares</code>: Assigns relative priority compared to other containers.</li>



<li><code>--cpuset-cpus</code>: Specifies the exact CPU cores the container can use.</li>
</ul>



<p>Example:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="docker run --cpus=0.5 my-container
" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #50FA7B">docker</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">run</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">--cpus=0.5</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">my-container</span></span>
<span class="line"></span></code></pre></div>



<p>This command limits the container to 50% of the CPU.</p>



<h4 class="wp-block-heading"><strong>Assigning Specific Cores</strong></h4>



<p>If you want a container to use specific cores:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="docker run --cpuset-cpus=&quot;0,1&quot; my-container
" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #50FA7B">docker</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">run</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">--cpuset-cpus=</span><span style="color: #E9F284">&quot;</span><span style="color: #F1FA8C">0,1</span><span style="color: #E9F284">&quot;</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">my-container</span></span>
<span class="line"></span></code></pre></div>



<p>This restricts the container to only use the first and second cores.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><strong>3. Setting Resource Limits in Docker Compose</strong></h2>



<p>For multi-container environments, resource limits can be defined in a <code>docker-compose.yml</code> file.</p>



<p>Example <code>docker-compose.yml</code> file:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="version: '3.7'
services:
  my-service:
    image: my-image
    deploy:
      resources:
        limits:
          memory: 512M
          cpus: '0.5'
        reservations:
          memory: 256M
          cpus: '0.25'
" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #50FA7B">version:</span><span style="color: #F8F8F2"> </span><span style="color: #E9F284">&#39;</span><span style="color: #F1FA8C">3.7</span><span style="color: #E9F284">&#39;</span></span>
<span class="line"><span style="color: #50FA7B">services:</span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #50FA7B">my-service:</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #50FA7B">image:</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">my-image</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #50FA7B">deploy:</span></span>
<span class="line"><span style="color: #F8F8F2">      </span><span style="color: #50FA7B">resources:</span></span>
<span class="line"><span style="color: #F8F8F2">        </span><span style="color: #50FA7B">limits:</span></span>
<span class="line"><span style="color: #F8F8F2">          </span><span style="color: #50FA7B">memory:</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">512</span><span style="color: #F1FA8C">M</span></span>
<span class="line"><span style="color: #F8F8F2">          </span><span style="color: #50FA7B">cpus:</span><span style="color: #F8F8F2"> </span><span style="color: #E9F284">&#39;</span><span style="color: #F1FA8C">0.5</span><span style="color: #E9F284">&#39;</span></span>
<span class="line"><span style="color: #F8F8F2">        </span><span style="color: #50FA7B">reservations:</span></span>
<span class="line"><span style="color: #F8F8F2">          </span><span style="color: #50FA7B">memory:</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">256</span><span style="color: #F1FA8C">M</span></span>
<span class="line"><span style="color: #F8F8F2">          </span><span style="color: #50FA7B">cpus:</span><span style="color: #F8F8F2"> </span><span style="color: #E9F284">&#39;</span><span style="color: #F1FA8C">0.25</span><span style="color: #E9F284">&#39;</span></span>
<span class="line"></span></code></pre></div>



<p>In this example:</p>



<ul class="wp-block-list">
<li>The memory limit is set to 512 MB,</li>



<li>The CPU usage limit is 50%.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><strong>4. Best Practices</strong></h2>



<p>When configuring resource limits, keep these practices in mind:</p>



<h3 class="wp-block-heading"><strong>4.1. Adjust According to Needs</strong></h3>



<p>Each container has different requirements. Analyze your application’s memory and CPU usage profile to set appropriate limits.</p>



<h3 class="wp-block-heading"><strong>4.2. Monitor and Optimize</strong></h3>



<p>Use Docker’s monitoring tools to track resource usage:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="docker stats
" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #50FA7B">docker</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">stats</span></span>
<span class="line"></span></code></pre></div>



<p>This command provides real-time CPU, memory, and network usage for all running containers.</p>



<h3 class="wp-block-heading"><strong>4.3. Avoid Overcommitment</strong></h3>



<p>If running multiple containers, ensure that their combined resource limits do not exceed your host machine&#8217;s capacity. Overcommitment can lead to system instability.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><strong>5. Advanced Configurations</strong></h2>



<h3 class="wp-block-heading"><strong>5.1. Using cgroups</strong></h3>



<p>Docker uses Linux control groups (cgroups) for resource management. For advanced configurations, you can manually modify cgroup parameters.</p>



<h3 class="wp-block-heading"><strong>5.2. Managing Resources in Docker Swarm</strong></h3>



<p>If you&#8217;re using Docker Swarm, resource limits can be set in the <code>deploy</code> section.</p>



<p>Example:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="deploy:
  resources:
    limits:
      memory: 1G
      cpus: &quot;1.0&quot;
" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #50FA7B">deploy:</span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #50FA7B">resources:</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #50FA7B">limits:</span></span>
<span class="line"><span style="color: #F8F8F2">      </span><span style="color: #50FA7B">memory:</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">1</span><span style="color: #F1FA8C">G</span></span>
<span class="line"><span style="color: #F8F8F2">      </span><span style="color: #50FA7B">cpus:</span><span style="color: #F8F8F2"> </span><span style="color: #E9F284">&quot;</span><span style="color: #F1FA8C">1.0</span><span style="color: #E9F284">&quot;</span></span>
<span class="line"></span></code></pre></div>



<h3 class="wp-block-heading"><strong>5.3. Kubernetes Integration</strong></h3>



<p>If managing Docker containers with Kubernetes, resource limits can be specified in the <code>resources</code> section:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="resources:
  limits:
    memory: &quot;512Mi&quot;
    cpu: &quot;0.5&quot;
" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #50FA7B">resources:</span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #50FA7B">limits:</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #50FA7B">memory:</span><span style="color: #F8F8F2"> </span><span style="color: #E9F284">&quot;</span><span style="color: #F1FA8C">512Mi</span><span style="color: #E9F284">&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #50FA7B">cpu:</span><span style="color: #F8F8F2"> </span><span style="color: #E9F284">&quot;</span><span style="color: #F1FA8C">0.5</span><span style="color: #E9F284">&quot;</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><strong>6. Advantages of Memory and CPU Limiting</strong></h2>



<ul class="wp-block-list">
<li><strong>Improved Performance:</strong> Ensures other applications have sufficient resources.</li>



<li><strong>System Stability:</strong> Prevents crashes caused by resource overconsumption.</li>



<li><strong>Enhanced Security:</strong> Stops misbehaving containers from overwhelming the system.</li>



<li><strong>Cost Efficiency:</strong> Especially in cloud environments, reducing resource waste lowers costs.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><strong>7. Use Cases</strong></h2>



<h3 class="wp-block-heading"><strong>Scenario 1: Web Server</strong></h3>



<p>For an NGINX web server, limit memory to 1 GB and CPU usage to 25%:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="docker run --memory=1g --cpus=0.25 nginx
" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #50FA7B">docker</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">run</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">--memory=1g</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">--cpus=0.25</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">nginx</span></span>
<span class="line"></span></code></pre></div>



<h3 class="wp-block-heading"><strong>Scenario 2: Resource-Intensive Application</strong></h3>



<p>For a machine learning application:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="docker run --memory=4g --cpus=2 my-ml-app
" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dracula" style="background-color: #282A36" tabindex="0"><code><span class="line"><span style="color: #50FA7B">docker</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">run</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">--memory=4g</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">--cpus=2</span><span style="color: #F8F8F2"> </span><span style="color: #F1FA8C">my-ml-app</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><strong>8. Conclusion</strong></h2>



<p>Limiting memory and CPU usage in Docker is a key aspect of optimizing container performance and ensuring efficient resource usage. This guide covered everything from basic commands to advanced configurations. Analyze your application’s needs, monitor resource usage, and continually optimize your settings.</p>



<p><strong>By implementing resource limits, you can build faster, more stable, and more reliable applications with Docker.</strong></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
