docker:守护进程响应错误:OCI 运行时创建失败:进程 init 的 cgroup 内存限制已超出
docker: Error response from daemon: OCI runtime create failed: cgroup memory limit exceeded for process init
ID: docker/cgroup-memory-limit-exceeded-for-process
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Docker 20.10.23 | active | — | — | — |
| Docker 24.0.7 | active | — | — | — |
| containerd 1.6.22 | active | — | — | — |
根因分析
通过 --memory 或 docker-compose mem_limit 设置的容器内存限制过低,无法满足初始进程的内存分配,导致 OOM 杀手在启动时立即终止容器。
English
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.
官方文档
https://docs.docker.com/config/containers/resource_constraints/#memory解决方案
-
临时运行容器而不设置内存限制,使用 '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>'。
无效尝试
常见但无效的做法:
-
60% 失败
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% 失败
While this avoids the error, it removes resource constraints, potentially causing host instability if the container consumes excessive memory.
-
80% 失败
The error occurs at runtime initialization, not during build; the base image size does not affect the process's memory allocation at start time.