如何排除 Docker 容器的 OOM killer 容器重启故障?
4809
Created On 05/29/25 21:30 PM - Last Modified 07/09/25 17:28 PM
Objective
- 查看以下 Docker 命令的输出:
-docker ps
-docker 检查
-docker 统计信息
目的是确定 Docker 容器是否已重新启动,如果是,则 OOM killer 是否是导致容器重新启动的原因。
Environment
- Linux/macOS 机器
- Docker 运行时
Procedure
- 码头工人 ps
1.此命令将显示您环境中正在运行的容器。
2. 必须检查STATUS列来确定容器已存在多长时间
一直健康并且能正常跑步。
3. 运行时间将证明容器可能在 2 小时前重新启动/重新创建(在这种情况下) - docker 检查
1. 下面显示的代码片段是从该命令的输出中截取的,它确认OOM killer是导致容器被关闭/杀死的原因。
容器最后一次被杀死是在FinishedAt": "2025-05-19T13:42:31.566652718Z由 OOM 终止进程。
2.此命令的输出中还有一个片段或一行提到“RestartCount”:41。 - docker 统计信息
1.此命令将显示为容器设置的 cgroup 限制,该限制很可能被超出,从而导致重启。
解决:
Docker更新 命令是一个强大的实用程序,它允许您修改某些资源限制和配置选项运行容器无需停止并重新创建它们。这对于根据工作负载动态调整资源分配非常有用。
这是docker 更新的方式 工作,特别是与资源限制相关的工作:
-
内存限制:
--memoryor-m: Sets the maximum amount of memory the container can use (e.g.,512m,2g). If a container exceeds this limit, it may be OOMKilled (Out Of Memory Killed) by the system.--memory-swap: The total amount of memory and swap the container can use. If you set--memorybut not--memory-swap, the container can use swap space up to twice the memory limit (if host swap is enabled). To prevent a container from using any swap, set--memory-swapto the same value as--memory
- 在此用例中,4Gi 内存不足以满足容器的需求,因此会出现容器重启的情况。您可以使用以下命令进行更改:
docker update --memory=6g <name-of-the-container>
请注意:
- Memory Swap (
--memory-swap): Allows you to update the total memory the container can use (RAM + Swap).- If
--memory-swapis set to the same value as--memory, the container cannot use swap. - If
--memory-swapis-1, the container can use unlimited swap (up to the host's available swap). - If
--memoryis set and--memory-swapis not, the container can use swap up to the same amount as the memory limit (total 2x the memory limit)
- If
Additional Information
可以使用以下命令检查 OOM 消息:
sudo dmesg -T | grep -i "oom-killer" or sudo journalctl -k | grep -i "oom-killer"