# 容器以代码 139 退出（段错误）

- **ID:** `docker/container-exited-139-segfault`
- **领域:** docker
- **类别:** runtime_error
- **错误码:** `139`
- **验证级别:** ai_generated
- **修复率:** 72%

## 根因

容器内应用程序因段错误崩溃，通常由内存访问违规、栈溢出或不兼容的共享库引起。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Docker 24.0.5 | active | — | — |
| Linux 5.10.0 | active | — | — |
| glibc 2.35 | active | — | — |

## 解决方案

1. ```
   在容器内启用核心转储：添加 '--ulimit core=-1' 并挂载卷以存储核心文件：'docker run --ulimit core=-1 -v /tmp/cores:/cores myapp'。然后使用 gdb 分析：'gdb /path/to/binary /cores/core'。
   ```
2. ```
   检查不兼容的共享库：在容器内运行 'ldd /usr/bin/myapp'。如果任何库显示 'not found'，使用正确的库版本重建镜像。
   ```

## 无效尝试

- **Increase container memory limit with --memory** — Segfault is often not due to memory limit; increasing memory may mask the issue but not fix the root cause. (60% 失败率)
- **Rebuild the image with --no-cache** — Cache invalidation does not fix application logic errors or library incompatibilities. (70% 失败率)
- **Run container with --security-opt seccomp=unconfined** — Unconfined seccomp may allow more syscalls but does not prevent segfaults from application bugs. (55% 失败率)
