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

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

## 根因

容器内的进程尝试访问不允许的内存，通常由于不兼容的二进制架构、内存损坏或内存限制不足。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Docker 24.0.7 | active | — | — |
| Docker 25.0.0 | active | — | — |
| Alpine 3.18 | active | — | — |

## 解决方案

1. ```
   Check binary architecture compatibility: run 'file /path/to/binary' inside container. If mismatch, rebuild for correct architecture (e.g., use --platform linux/amd64).
   ```
2. ```
   Increase memory limit and add swap: docker run -m 512m --memory-swap 1g your_image
   ```
3. ```
   If using gdb or valgrind, debug the segfault: docker run --cap-add=SYS_PTRACE --security-opt seccomp=unconfined your_image gdb /path/to/binary
   ```

## 无效尝试

- **docker run -m 2g your_image** — Increasing memory limit alone does not fix the underlying segfault cause. (80% 失败率)
- **docker build --no-cache -t your_image .** — Rebuilding the image without fixing architecture mismatch still produces the same binary. (70% 失败率)
- **docker run --user root your_image** — Running as root does not prevent segfaults caused by memory corruption. (90% 失败率)
