Understanding Docker Container Exit Codes

本文详细解析了Docker容器中常见的退出代码及其含义,包括0、1、137、139和143等,阐述了这些代码如何帮助诊断容器运行失败的原因,并提供了查找退出代码的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The most common exit codes, what they mean, and what causes them

 

Sandeep MadamanchiSandeep Madamanchi

Follow

Oct 21, 2019 · 4 min read

 

 

 

 

Docker container exit code — how to use them for troubleshooting?

It’s one of the most common question that I come across: “Why is my container not running?”

Can docker container exit codes help troubleshoot this issue?

The first step in answering this question is to identify the exit code for the docker container. The exit code may give a hint as to what happened to stop the container running. This article lists the most common exit codes when working with docker containers and aims to answer two important questions:

  • What does this specific exit code mean?
  • What action caused this exit code?

This will ultimately help answer the original question: “Why is my container not running?”


How to Find Exit Codes

Option 1: List all containers that exited

docker ps --filter "status=exited"

Option 2: Grep by container name

docker ps -a grep <container-name>Example: docker ps -a | grep hello-world

Option 3: Inspect by container id

docker inspect <container-id> --format='{{.State.ExitCode}}'Example: docker inspect ca6cbb290468 --format='{{.State.ExitCode}}'

Exit Codes

Common exit codes associated with docker containers are:

  • Exit Code 0: Absence of an attached foreground process
  • Exit Code 1: Indicates failure due to application error
  • Exit Code 137: Indicates failure as container received SIGKILL (Manual intervention or ‘oom-killer’ [OUT-OF-MEMORY])
  • Exit Code 139: Indicates failure as container received SIGSEGV
  • Exit Code 143: Indicates failure as container received SIGTERM

Exit Code 0

  • Exit code 0 indicates that the specific container does not have a foreground process attached.
  • This exit code is the exception to all the other exit codes to follow. It does not necessarily mean something bad happened.
  • Developers use this exit code if they want to automatically stop their container once it has completed its job.

Here is an example using the public docker container — “hello-world”. If you have docker installed on your system or VM instance, run this:

docker run hello-world

You will get a message, “Hello from docker!” but try to find the container using this code:

docker ps -a | grep hello-world

You’ll notice that the container exited and the exit code is 0. This is because the container does not have any foreground process attached, such as a Java process or a shell process that runs until a SIGTERM event occurs

Exit code 0

Exit Code 1

  • Indicates that the container stopped due to either an application error or an incorrect reference in Dockerfile to a file that is not present in the container.
  • An application error can be as simple as “divide by 0” or as complex as “Reference to a bean name that conflicts with existing, non-compatible bean definition of same name and class.”
  • An incorrect reference in Dockerfile to a file not present in the container can be as simple as a typo (the example below has sample.ja instead of sample.jar)

Exit Code 1

Exit Code 137

  • This indicates that container received SIGKILL
  • A common event that initiates a SIGKILL is a docker kill. This can be initiated either manually by user or by the docker daemon:
docker kill <container-id>
  • docker kill can be initiated manually by the user or by the host machine. If initiated by host machine, then it is generally due to being out of memory. To confirm if the container exited due to being out of memory, verify docker inspect against the container id for the section below and check if OOMKilled is true (which would indicate it is out of memory):
"State": {
 "Status": "exited",
 "Running": false,
 "Paused": false,
 "Restarting": false,
 "OOMKilled": true,
 "Dead": false,
 "Pid": 0,
 "ExitCode": 137,
 "Error": "",
 "StartedAt": "2019-10-21T01:13:51.7340288Z",
 "FinishedAt": "2019-10-21T01:13:51.7961614Z"
}

Exit Code 139

  • This indicates that container received SIGSEGV
  • SIGSEGV indicates a segmentation fault. This occurs when a program attempts to access a memory location that it’s not allowed to access, or attempts to access a memory location in a way that’s not allowed.
  • From the Docker container standpoint, this either indicates an issue with the application code or sometimes an issue with the base images used by the container.

Exit Code 143

  • This indicates that container received SIGTERM.
  • Common events that initiate a SIGTERM are docker stop or docker-compose stop. In this case there was a manual termination that forced the container to exit:
docker stop <container-id>
OR
docker-compose down <container-id>
  • Note: sometimes docker stop can also result in exit code 137. This typically happens if the application tied to the container doesn’t handle SIGTERM — the docker daemon waits ten seconds then issues SIGKILL

Some uncommon exit codes with Docker containers (typically with shell script usage)

  • Exit Code 126: Permission problem or command is not executable
  • Exit Code 127: Possible typos in shell script with unrecognizable characters
Docker容器是通过Docker镜像创建的可运行实例。Docker容器是在Docker镜像的基础上建立一个可读写的容器层。镜像负责存储和分发应用程序,而容器负责运行应用程序。常用的Docker容器指令包括: - `docker container ls`:列出所有正在运行的容器的列表。 - `docker container ls -a`:列出所有容器的列表,包括正在运行的和已退出的容器。\[2\] 如果你想运行一个Docker容器,可以使用`docker run <IMAGE STRING>`命令,其中`<IMAGE STRING>`是你想要运行的镜像名称或标签。\[2\] 如果你想删除所有已退出的容器,可以使用以下命令: 1. 列出所有状态为exited的容器的ID:`docker container ls -f "status=exited" -q`\[1\] 2. 根据上述ID删除所有容器:`docker rm $(docker container ls -f "status=exited" -q)`\[1\] #### 引用[.reference_title] - *1* *2* [Docker Container介绍](https://blog.csdn.net/lt326030434/article/details/90384228)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [三、Docker相关概念-container](https://blog.csdn.net/qq_26707371/article/details/101467711)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值