By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Set resource limits for Docker containers to prevent them from consuming excessive CPU or memory. docker run -c/--cpu-shares/-m/--memory
docker stats
docker run -it --cpu-shares=1024 --memory=512m ubuntu /bin/bash
docker stats <container_id>
docker rm <container_id>
docker run -it --cpu-shares=1024 --memory=512m --memory-swap=0 ubuntu /bin/bash
Purpose: Limit a container's CPU usage to 50% of the host's CPU.Command: docker run -it --cpu-shares=512 ubuntu /bin/bash Flags: -c or --cpu-shares sets the CPU shares, and --cpu-shares=512 sets the value to 512.
docker run -it --cpu-shares=512 ubuntu /bin/bash
-c
--cpu-shares
--cpu-shares=512
Purpose: Limit a container's memory usage to 256 MB.Command: docker run -it --memory=256m ubuntu /bin/bash Flags: -m or --memory sets the memory limit, and --memory=256m sets the value to 256 MB.
docker run -it --memory=256m ubuntu /bin/bash
-m
--memory
--memory=256m
Purpose: Limit a container's memory swap usage to 0 MB.Command: docker run -it --memory=512m --memory-swap=0 ubuntu /bin/bash Flags: -m or --memory sets the memory limit, --memory-swap sets the swap limit, and --memory-swap=0 sets the value to 0 MB.
docker run -it --memory=512m --memory-swap=0 ubuntu /bin/bash
--memory-swap
--memory-swap=0
Error message: "Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:459: container init caused: running prestart hook 0 caused: failed to start hook: OCI runtime error: container_linux.go:380: starting container process caused: process_linux.go:459: container init caused: running prestart hook 0 caused: failed to start hook: failed to execute prestart hook: exit status 1".Why it happens: The container's resource limits are too low.One-line fix: docker run -it --cpu-shares=1024 --memory=512m ubuntu /bin/bash
Error message: "Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:459: container init caused: running prestart hook 0 caused: failed to start hook: OCI runtime error: container_linux.go:380: starting container process caused: process_linux.go:459: container init caused: running prestart hook 0 caused: failed to start hook: failed to execute prestart hook: exit status 1".Why it happens: The container's memory swap limit is too low.One-line fix: docker run -it --cpu-shares=1024 --memory=512m --memory-swap=0 ubuntu /bin/bash
Error message: "Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:459: container init caused: running prestart hook 0 caused: failed to start hook: OCI runtime error: container_linux.go:380: starting container process caused: process_linux.go:459: container init caused: running prestart hook 0 caused: failed to start hook: failed to execute prestart hook: exit status 1".Why it happens: The container's resource limits are too high.One-line fix: docker run -it --cpu-shares=512 --memory=256m ubuntu /bin/bash
docker run -it --cpu-shares=512 --memory=256m ubuntu /bin/bash
Situation: A container is consuming 100% of the host's CPU.Command to run: docker stats Correct response: The container's CPU usage is below 100%.
Situation: A container is consuming 100% of the host's memory.Command to run: docker stats Correct response: The container's memory usage is below 100%.
Situation: A container is consuming 100% of the host's memory swap.Command to run: docker stats Correct response: The container's memory swap usage is below 100%.
--cpu-quota
docker run
docker rm
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.