docker: Error response from daemon: OCI runtime create failed: cgroup memory limit exceeded for process init
ID: docker/cgroup-memory-limit-exceeded-for-process
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Docker 20.10.23 | active | — | — | — |
| Docker 24.0.7 | active | — | — | — |
| containerd 1.6.22 | active | — | — | — |
Root Cause
The container's memory limit set via --memory or docker-compose mem_limit is too low for the initial process allocation, causing the OOM killer to terminate the container immediately on start.
generic中文
通过 --memory 或 docker-compose mem_limit 设置的容器内存限制过低,无法满足初始进程的内存分配,导致 OOM 杀手在启动时立即终止容器。
Official Documentation
https://docs.docker.com/config/containers/resource_constraints/#memoryWorkarounds
-
90% success Check the container's baseline memory usage by running it without memory limits temporarily using 'docker run --rm <image> free -m' or 'docker stats', then set --memory to at least 1.5x that value. For example, if baseline is 200MB, use '--memory=300m'.
Check the container's baseline memory usage by running it without memory limits temporarily using 'docker run --rm <image> free -m' or 'docker stats', then set --memory to at least 1.5x that value. For example, if baseline is 200MB, use '--memory=300m'.
-
85% success If using docker-compose, add 'mem_limit: 512m' or higher in the service definition, and also set 'mem_reservation: 256m' to ensure minimum memory availability.
If using docker-compose, add 'mem_limit: 512m' or higher in the service definition, and also set 'mem_reservation: 256m' to ensure minimum memory availability.
-
80% success For containers with high memory spikes on startup (e.g., Java apps), use '--memory-swap' to allow swap space. Example: 'docker run --memory=256m --memory-swap=512m <image>'.
For containers with high memory spikes on startup (e.g., Java apps), use '--memory-swap' to allow swap space. Example: 'docker run --memory=256m --memory-swap=512m <image>'.
中文步骤
临时运行容器而不设置内存限制,使用 'docker run --rm <image> free -m' 或 'docker stats' 检查基线内存使用量,然后将 --memory 设置为至少 1.5 倍。例如,如果基线为 200MB,使用 '--memory=300m'。
如果使用 docker-compose,在服务定义中添加 'mem_limit: 512m' 或更高,并设置 'mem_reservation: 256m' 以确保最小内存可用性。
对于启动时内存峰值高的容器(例如 Java 应用),使用 '--memory-swap' 允许交换空间。示例:'docker run --memory=256m --memory-swap=512m <image>'。
Dead Ends
Common approaches that don't work:
-
60% fail
If the container's process requires more memory than the increased limit (e.g., 256MB for a Node.js app), a small increment may still be insufficient, causing the same error.
-
30% fail
While this avoids the error, it removes resource constraints, potentially causing host instability if the container consumes excessive memory.
-
80% fail
The error occurs at runtime initialization, not during build; the base image size does not affect the process's memory allocation at start time.