When a container will not stay up, check the same things in the same order every time.
1. docker ps -a state and exit code
2. docker logs NAME what the program said before it exited
3. docker ps -a --format the command and restart policy it was created with
4. docker run --rm IMAGE ... look inside the image with a throwaway container
5. docker rm -f NAME remove the broken container
6. docker run ... run a correct one with the same name and options
7. docker ps / docker logs confirm it stays up1. State and exit code
| STATUS | Meaning |
|---|---|
Exited (0) | PID 1 finished; maybe it had no work to do |
Exited (1) / (2) | the program failed; Python exits 2 when it cannot open its script |
Exited (127) | the command does not exist in the image |
Restarting (N) | it keeps exiting with N, and a restart policy keeps starting it |
2. Logs
docker logs --tail 20 NAME shows the last attempt. In a restart loop, the same error repeats once per attempt.
3. How it was created
docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Command}}'
docker inspect -f '{{.Path}} {{.Args}}' worker
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}}' workerdocker ps truncates the COMMAND column. The inspect templates print it in full. Chapter 3 covers docker inspect.
4. Look inside the image
docker exec needs a running container, and a crashing one is stopped most of the time. Start a throwaway container from the same image with a command you choose:
docker run --rm IMAGE ls /app
docker run --rm IMAGE cat /app/README.txt--rm keeps these from piling up as stopped containers.
5-7. Replace it
A container's command, image and options are fixed at creation, so restart cannot fix a wrong command. Remove the container and run a new one with the same name, image and restart policy and the corrected command:
docker rm -f NAME
docker run -d --name NAME --restart POLICY IMAGE [correct command]Leaving out the command uses the image's default command.
Confirm: docker ps shows Up and the time keeps growing, and docker logs shows a clean start. Clean up anything you created while investigating.