Docker exit codes indicate why a container stopped running. Here’s a breakdown of common exit codes, which can be essential for troubleshooting:

How to Check Docker Container Exit Codes

To view the exit code of a stopped container:

docker inspect <container_name_or_id> --format='{{.State.ExitCode}}'

Common Docker Exit Codes

Exit Code 0: Success

  • Meaning: The container completed its process successfully.
  • Example: If a simple container runs a script that prints “Hello, World!” and finishes without errors, it will exit with code 0.
docker run hello-world

Exit Code 1: Generic Error

  • Meaning: A generic catch-all error code, typically related to application logic.
  • Example: A Python container runs a script with a syntax error, and it throws an exception, leading to exit code 1.
docker run python:3.8 python -c 'print(Hello World)'

This will cause a syntax error because the string Hello World lacks quotes.

Exit Code 137: Out of Memory (OOM) or Killed

  • Meaning: The container was terminated by the system, usually because it ran out of memory or was manually killed with the kill -9 signal.
  • Example: A Java application using excessive memory causes the system to terminate it due to memory constraints.
docker run --memory="50m" openjdk java -Xmx512m MyApp

Exit Code 139: Segmentation Fault

  • Meaning: A segmentation fault occurred, often due to a low-level issue with memory access.
  • Example: A C program attempts to access memory incorrectly, causing the container to crash.
docker run gcc /bin/bash -c "echo 'int main(){int *p = 0; *p = 1;}' > segfault.c && gcc segfault.c -o segfault && ./segfault"

Exit Code 143: Gracefully Stopped by SIGTERM

  • Meaning: The container received the SIGTERM signal and stopped gracefully.
  • Example: A container running a web server is stopped using docker stop, which sends a SIGTERM signal.
docker stop <container_name_or_id>

Exit Code 127: Command Not Found

  • Meaning: The command inside the container could not be found or executed.
  • Example: Running a container that tries to execute a non-existent command.
docker run ubuntu sh -c 'non_existing_command'

Exit Code 126: Command Cannot Be Invoked

  • Meaning: The command exists but cannot be executed, usually due to permission issues or an invalid executable.
  • Example: Running a script inside a container without execution permissions
docker run ubuntu bash -c 'chmod -x /bin/ls && /bin/ls'

Exit Code 255: Unknown Error

  • Meaning: An unknown or application-specific error occurred.
  • Example: An application returns an exit code that isn’t one of the standard codes, often for app-specific reasons.

Exit Code 3: Misuse of Shell Built-ins

  • Meaning: This error occurs when there’s improper use of shell commands or a syntax error in the script.
  • Example: A bash script with invalid syntax or misuse of a shell built-in command can cause this error.
docker run ubuntu bash -c 'exit 3'

Exit Code 125: Docker Command Itself Failed

  • Meaning: Docker failed to run the container due to an issue with the Docker command itself. This could be caused by incorrect options or an issue with Docker’s internals.
  • Example: Trying to run a container with a bad option:
docker run --wrong-option ubuntu

Exit Code 130: Script Terminated by Control+C (SIGINT)

  • Meaning: The container was terminated by the user pressing Ctrl+C, sending a SIGINT (interrupt signal).
  • Example: If a user manually stops a running container by interrupting the process:
docker run ubuntu sleep 100
# Press Ctrl+C to stop the container

Exit Code 137: Forced Kill (SIGKILL)

  • Meaning: Similar to Exit Code 137, but this specific signal (SIGKILL) happens when the container is forcefully killed, often using the docker kill command.
  • Example: Killing a running container forcibly:
docker run ubuntu sleep 100
docker kill <container_name_or_id>
  • Exit Code 143: Graceful Shutdown by SIGTERM
  • Meaning: As mentioned, this occurs when the container receives the SIGTERM signal and shuts down gracefully, often due to docker stop.
  • Exit Code 255: Custom or Application-Specific Code
  • Meaning: Some applications might use exit code 255 for their own specific errors. This isn’t a standard code but is often used to indicate an unknown or custom application-level failure.
  • Example: Running a custom app that returns a 255 exit code:
docker run my_custom_app --exit 255

Exit Code 129: Hangup (SIGHUP)

  • Meaning: This indicates that the container received a hangup signal (SIGHUP), often from the terminal or session being closed.
  • Example: If a session gets disconnected or a process is terminated remotely:
docker run ubuntu bash -c 'kill -1 $'

Exit Code 137: Out of Memory (OOM)

  • Meaning: Although already mentioned, this exit code can also be caused specifically by an out-of-memory (OOM) event, where the container is killed by the system due to excessive memory consumption.
  • Example: Running a memory-intensive Java application:
docker run --memory="100m" openjdk java -Xmx512m -jar myapp.jar

Exit Code 141: Write to a Pipe with No Reader (SIGPIPE)

  • Meaning: The container received a SIGPIPE signal, which happens when it attempts to write to a pipe that has no active reader.
  • Example: A process writes to a closed pipe, leading to exit code 141.
docker run ubuntu bash -c 'echo "data" | false'

Exit Code 143: Gracefully Killed (SIGTERM)

  • Meaning: The container was terminated using a SIGTERM signal, usually from docker stop. This allows the process to exit gracefully and clean up resources before shutting down.
  • Example: If you stop a container gracefully:
docker stop <container_name_or_id>

Other Exit Codes

  • Exit Code 2: Misuse of shell built-ins, or a command line syntax error.
  • Exit Code 126: Permission denied when trying to run a command.
  • Exit Code 128: Invalid argument to exit, such as using an invalid signal in the kill command.

These exit codes provide important clues when diagnosing issues in Docker containers, helping pinpoint whether errors are related to application logic, system resources, or command execution failures.

Related Article:

How to Troubleshoot Docker Issues with Real Examples

Reclaiming Docker Cache Disk Space with Docker Builder Prune


Discover more from Tech Insights & Blogs by Rahul Ranjan

Subscribe to get the latest posts sent to your email.

Leave a comment

Trending