Debugging a running container

Intermediate · 29 min

A method for broken containers: read state, exit codes and logs first, then get inside with exec, docker debug, a namespace-sharing sidecar or nsenter.

Before thisInteractive, detached and execContainer logs and logging driversInspecting containers: inspect, top, stats and events

Cloud Native Rejekts · 29 min

Debugging a running container

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Cloud Native RejektsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Start outside the container: `docker ps -a`, `docker inspect` for State, ExitCode, OOMKilled, RestartCount and health logs, then `docker logs`.
  • Exit codes carry information. 125 means Docker itself failed, 126 the command couldn't be run, 127 it wasn't found, and 128 plus N means signal N, so 137 is SIGKILL and 143 is SIGTERM.
  • For a container that crashes on start, override the entrypoint, `docker run --rm -it --entrypoint sh image`, and run the command by hand.
  • `docker exec` needs tools inside the image. Slim and distroless images often have no shell at all.
  • `docker debug` attaches a toolbox shell to any container or image without changing it. It is part of Docker Desktop and free from version 4.49.
  • A sidecar that joins the container's namespaces, `--network container:<name>` and `--pid container:<name>`, brings your own tools, such as netshoot, to a shell-less container.
  • On a Linux host, `nsenter -t <pid>` enters a container's namespaces using the host's tools. `docker cp` and `docker diff` let you inspect files without a shell.

Look before you go in

A container misbehaves: it won’t start, it restarts in a loop, or it runs but returns errors. The instinct is to exec into it immediately. Resist that. A container that keeps dying isn’t there to exec into, and Docker already recorded most of what you need. Think of it as reading the patient’s chart before examining them.

Step 1: state, exit code, logs

docker ps -a --filter name=api           # running, exited, restarting?
docker inspect api --format '{{json .State}}' | jq
docker logs --tail 100 --timestamps api
docker events --since 10m --filter container=api

.State holds Status, ExitCode, Error, OOMKilled, StartedAt and FinishedAt, and .Health.Log with the output of recent health checks. .RestartCount on the container tells you whether a restart policy is quietly hiding a crash loop. docker events shows the sequence, die, start, die, start, which docker logs alone doesn’t.

The exit code is the first clue. From the docker run reference and the shell convention:

Code Meaning
0 the main process finished normally
1 the application reported an error, read the logs
125 Docker itself failed, for example a bad flag
126 the command exists but can’t be invoked, such as a directory or a missing execute bit
127 the command wasn’t found in the container
137 128 + 9: SIGKILL, from docker kill, a stop timeout, or the OOM killer if OOMKilled is true
139 128 + 11: SIGSEGV, the process crashed
143 128 + 15: SIGTERM, a normal docker stop

127 with a script as the entrypoint often means its interpreter is missing: a #!/bin/bash line in an Alpine image that only has sh. 137 with OOMKilled: false after docker stop means the process ignored SIGTERM for the whole grace period, a PID 1 problem.

Step 2: reproduce the start by hand

For a container that dies before you can reach it, start the same image with a shell instead of its command:

docker run --rm -it --entrypoint sh registry.example.com/api:1.4.2
# now run the real command and watch it fail

Use the same environment, mounts and network as the real container, or docker compose run --rm --entrypoint sh api to inherit them from the Compose file.

Step 3: get inside a running container

docker exec -it api sh runs a new process in the container’s namespaces. It’s the right first tool, but it only has what the image has. Slim images lack ps, curl and netstat. Distroless images lack a shell entirely. I listed every file in the current gcr.io/distroless/static-debian13:nonroot image: 1,453 entries in under a megabyte of compressed layers, with /etc/passwd, CA certificates and time zone data, and nothing in bin, sbin or usr/bin. There is nothing to exec.

You have four ways around that.

docker debug. It gives you a shell in any container or image, even one with no shell, without modifying it. It brings its own toolbox with vim, htop, curl and others, and an install command for more from Nix packages. In a running container, file changes you make are visible to it, and the toolbox’s /nix directory never is. It ships with Docker Desktop and, per Docker’s docs, is available to all users from Desktop 4.49. Earlier versions needed a paid subscription.

docker debug api

A namespace-sharing sidecar. Start a tool container that joins the target’s namespaces. It sees the target’s network interfaces, ports and processes, but uses its own filesystem full of tools:

docker run --rm -it \
  --network container:api \
  --pid container:api \
  nicolaka/netshoot
# ss -tlnp, curl localhost:8000/health, tcpdump -i eth0, ps aux

With a shared PID namespace you can even read the target’s files through /proc/<pid>/root/, subject to permissions. This works anywhere Docker runs.

nsenter from the host. On a Linux host you can enter one namespace at a time with the host’s own tools, as the Linux namespaces lesson showed:

pid=$(docker inspect --format '{{.State.Pid}}' api)
sudo nsenter -t "$pid" -n ss -tlnp     # the container's sockets, the host's ss

Files without a shell. docker cp api:/app/config.yaml . copies a file out. docker diff api lists files the container added (A), changed (C) or deleted (D) in its writable layer, which is often the quickest way to find a log file or core dump it wrote, or a config it unexpectedly modified.

A short checklist

  1. docker ps -a and the exit code.
  2. docker inspect State and Health, docker logs, docker events.
  3. Crashes at start: --entrypoint sh and run it by hand.
  4. Running but wrong: exec, then docker debug or a sidecar if the image is bare.
  5. Network questions: netshoot sharing its network namespace.

Resist “fixing” a container in place with exec. Whatever you change is lost on the next recreate. Find the cause, fix the image or the configuration, and redeploy.

About the video

The embed is all of “How the heck do I debug distroless containers?” from Cloud Native Rejekts, November 2024, 29 minutes. The description promises a hands-on demonstration of ways to troubleshoot images with no shell, package manager or utilities, the hard half of this lesson. A talk with the same title appears on the DevOpsDays DFW channel under Eric Smalling’s name, which is how I know who is speaking. It has no chapters and I judged it from its description and title. It also covers Kubernetes ephemeral containers, which you can skip for now. It predates docker debug becoming free in Docker Desktop 4.49. YouTube reports no audio language, so English was judged from the title, description and English auto-captions. Chainguard’s short demo of docker debug in deeper shows that one tool on a distroless nginx.

What was run: every layer of gcr.io/distroless/static-debian13:nonroot was downloaded from its registry and its file list read, to confirm it has no shell. Exit codes 125 to 127 were checked against the docker run reference. No container was debugged, since no daemon was available. docker debug details are from its CLI reference and Docker’s feature data, checked 2026-11-28.

Where this fits

Debugging is for one container right now. Seeing all of them, all the time, is next: monitoring containers.

Check yourself

  1. 01docker run myapp exits immediately with status 127. What does that suggest?

    The command couldn't be found inside the container: a typo in CMD or ENTRYPOINT, a binary missing from the image, or a script whose interpreter (#!/bin/bash) doesn't exist in it.

  2. 02docker exec -it api sh fails with an error that sh isn't found. The image is distroless. What are two ways to get a shell next to the process?

    docker debug api, which brings its own toolbox; or docker run --rm -it --network container:api --pid container:api nicolaka/netshoot, a sidecar sharing its network and PID namespaces.

  3. 03A container keeps restarting and docker logs shows only the last attempt. Where else can you look?

    docker inspect for RestartCount, ExitCode and OOMKilled, docker events for the sequence of die and start events, and the health check log under State.Health if it has one.

  4. 04How do you see which files a running container has changed compared with its image?

    docker diff <container>. It lists added, changed and deleted paths in the container's writable layer.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark