DEV Community

Cover image for Your Docker container didn't vanish. Here's how to find out why it stopped
Dockerlinux
Dockerlinux

Posted on

Your Docker container didn't vanish. Here's how to find out why it stopped

"My container disappeared" is one of the most common things people say when they start with Docker. It almost never disappeared. It stopped. Here's the five-step routine to find out why.

1. List all containers, not just the running ones

$ docker ps
CONTAINER ID   IMAGE   COMMAND   CREATED   STATUS   PORTS   NAMES

$ docker ps -a
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS                     PORTS   NAMES
3f2a9c1e8b7d   nginx:1.27   "/docker-entrypoint.…"   10 minutes ago   Exited (1) 2 minutes ago           web
Enter fullscreen mode Exit fullscreen mode

docker ps only shows running containers. -a shows all of them.

2. Read the exit code

The number in Exited (…) is the exit code of the container's main process. You can also ask for it directly:

docker inspect --format '{{.State.ExitCode}}' web
Enter fullscreen mode Exit fullscreen mode

A few you'll see often:

Exit code Usually means
0 The main process finished normally (a one-off job, or a command with nothing to keep it running)
1 The app crashed or failed to start
137 Killed with SIGKILL (128 + 9), often because it ran out of memory
143 Stopped with SIGTERM (128 + 15), for example by docker stop

For a 137, check whether the kernel's out-of-memory killer did it:

docker inspect --format '{{.State.OOMKilled}}' web
Enter fullscreen mode Exit fullscreen mode

3. Read the logs before restarting

docker logs --tail 50 web
Enter fullscreen mode Exit fullscreen mode

The last lines usually name the problem: a missing environment variable, a port already in use, a config file that isn't there. Restarting first just hides the error until it happens again.

4. Remember: a container lives as long as its main process

If the main process ends, the container ends, even if nothing went wrong. That's why docker run ubuntu exits straight away: it starts a shell, the shell has no input, and it finishes. Long-running services (a web server, a database) keep their main process in the foreground on purpose.

5. Bring it back, and make it come back by itself

docker start web
Enter fullscreen mode Exit fullscreen mode

If it stopped because the machine rebooted, give it a restart policy so it comes back on its own next time:

docker update --restart unless-stopped web
Enter fullscreen mode Exit fullscreen mode

unless-stopped restarts the container after a reboot or a crash, unless you stopped it yourself.

The short version

docker ps -a → exit code → docker logs → fix → docker start, and set a restart policy for anything that must survive a reboot.


If you'd like to practise this on a pretend production server, DockerLinux (my free browser game for learning Linux and Docker) has an on-call incident built around exactly this situation: the server rebooted and the shop is down.

What's the strangest reason you've found for a container that "disappeared"? 👇

Top comments (0)