# docker：守护进程响应错误：OCI 运行时创建失败：进程 init 的 cgroup 内存限制已超出

- **ID:** `docker/cgroup-memory-limit-exceeded-for-process`
- **领域:** docker
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

通过 --memory 或 docker-compose mem_limit 设置的容器内存限制过低，无法满足初始进程的内存分配，导致 OOM 杀手在启动时立即终止容器。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Docker 20.10.23 | active | — | — |
| Docker 24.0.7 | active | — | — |
| containerd 1.6.22 | active | — | — |

## 解决方案

1. ```
   临时运行容器而不设置内存限制，使用 'docker run --rm <image> free -m' 或 'docker stats' 检查基线内存使用量，然后将 --memory 设置为至少 1.5 倍。例如，如果基线为 200MB，使用 '--memory=300m'。
   ```
2. ```
   如果使用 docker-compose，在服务定义中添加 'mem_limit: 512m' 或更高，并设置 'mem_reservation: 256m' 以确保最小内存可用性。
   ```
3. ```
   对于启动时内存峰值高的容器（例如 Java 应用），使用 '--memory-swap' 允许交换空间。示例：'docker run --memory=256m --memory-swap=512m <image>'。
   ```

## 无效尝试

- **** — 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. (60% 失败率)
- **** — While this avoids the error, it removes resource constraints, potentially causing host instability if the container consumes excessive memory. (30% 失败率)
- **** — The error occurs at runtime initialization, not during build; the base image size does not affect the process's memory allocation at start time. (80% 失败率)
