skip to Main Content

I use docker-compose to maintain a project, and set restart=always to all the docker service.

But my API host crashed twice today, I look through the logs docker logs -f <container-id>. No cue found about the crash reason.

The logs guide me the main process was killed then restarted.

how to get the last unexpected exit code ?

Thanks.

2

Answers


  1. You can get the IDs and exit codes of exited containers with this command:

    docker inspect --format "{{.ID}} {{.State.ExitCode}}" $(docker ps -a --filter "status=exited" --format {{.ID}})
    

    Explanation:

    • docker ps -a --filter "status=exited" --format {{.ID}} will output containers IDs which state is exited
    • docker inspect --format "{{.ID}} {{.State.ExitCode}}" then will print IDs and exit codes of those containers
    Login or Signup to reply.
  2. Get the Container ID: First, you need to know the ID or name of the Docker container for which you want to retrieve the exit code. You can list all running containers along with their IDs using:

    docker ps
    

    Inspect the Container: Once you have the container ID, you can inspect it using the following command:

    docker inspect <container_id>
    

    Filter the Output: The docker inspect command returns a JSON object containing various details about the container. You can use tools like jq or filter the output using grep or other text processing utilities to extract the exit code.

    For example, you can use jq to filter the output and extract the exit code:

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

    Alternatively, you can use grep to filter the output:

    docker inspect <container_id> | grep '"ExitCode":'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search