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
You can get the IDs and exit codes of exited containers with this command:
Explanation:
docker ps -a --filter "status=exited" --format {{.ID}}
will output containers IDs which state isexited
docker inspect --format "{{.ID}} {{.State.ExitCode}}"
then will print IDs and exit codes of those containersGet 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:
Inspect the Container: Once you have the container ID, you can inspect it using the following command:
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:
Alternatively, you can use grep to filter the output: