SoFunction
Updated on 2025-04-08

Checking and analysis of status code execution of Docker container command

1. Execute the command manually and view the exit status code

After executing a command in a Docker container, we can view the exit status code of the command in two ways.

Method 1: Use echo $?

After running the command in the container, use echo $? to view the exit status code of the command. This is a very straightforward way to quickly tell us whether the command is successfully executed. For example, we can execute a curl command in the container to check the health status of the service and match the expected output via grep. If grep finds a matching content, the exit status code will be 0, indicating that the command is successfully executed; if no matching content is found, the exit status code will be 1, indicating that the command has failed.

docker exec -it <container_name> /bin/sh
# Example: Run the commandcurl -s http://localhost:8080/actuator/health | grep '"status":"UP"'
# Check the exit status codeecho $?

Method 2: Use docker inspect

Docker's health check mechanism records the execution status and exit code of the health check command. We can view this information through the docker inspect command. This command outputs a JSON-formatted string containing a detailed log of health checks, including the start time, end time, exit code and output information of each check.

docker inspect --format='{{json .}}' <container_name>

Output example:

{
  "Status": "unhealthy",
  "FailingStreak": 2,
  "Log": [
    {
      "Start": "2024-12-15T10:00:00.123456789Z",
      "End": "2024-12-15T10:00:01.123456789Z",
      "ExitCode": 1,
      "Output": "command failed: grep: no match"
    },
    {
      "Start": "2024-12-15T10:00:10.123456789Z",
      "End": "2024-12-15T10:00:11.123456789Z",
      "ExitCode": 1,
      "Output": "command failed: curl: (7) Failed to connect to localhost port 8080"
    }
  ]
}

In this output,ExitCodeThe field indicates the exit status code of the command, andOutputThe field provides standard output or error information for the command.

2. Check the meaning of the exit status code

Understanding the meaning of exit status codes is critical to diagnosing problems and optimizing health check logic. Here are some common status codes and their meanings:

  • 0: Successfully executed.
  • 1: General error, e.g.grepNo matching content.
  • 7: Unable to connect to the specified host, e.g.curlmistake.
  • 28: The request timed out, e.g.curlTimeout time exceeded.
  • 127: The command was not found, e.g.curlorgrepDoes not exist.

3. Local verification health check command

Before adjusting the health check logic, we can run on the host withFor the same command, manually check the exit status code. This helps us verify that the commands work as expected and ensure the correctness of the health check logic.

# Sample commandcurl -s http://localhost:8080/actuator/health | grep '"status":"UP"'
# Check the exit status codeecho $?

If the output is 0, it means that the health check has been passed; if the output is non-0, it means that the health check has failed, and further check of the service status or command logic is required.

4. Adjust the health check configuration

According to the meaning of the exit status code, we can adjust itIn-houseConfigure to ensure that the health check command logic is correct. For example, if the service starts slowly, we can increasestart_periodandtimeoutto avoid premature failure of health checks.

healthcheck:
  test:
    [
      "CMD-SHELL",
      'curl -s http://localhost:8080/actuator/health | grep ''"status":"UP"''',
    ]
  interval: 10s
  timeout: 5s
  retries: 3
  start_period: 30s

In this way, we can ensure that the Docker container does not undergo a health check until the service is fully started and ready to accept the request, thus avoiding unnecessary service restarts.

This is the article about the inspection and analysis of Docker container command execution status code. For more related Docker container command execution status code content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!