Lesson 18 / 28

Inspecting & Debugging Containers

Debug running containers with docker logs, exec, inspect and stats, and read exit codes when a container stops.

Logs and exec

logs shows output, exec opens a shell in a running container, and inspect shows full configuration.

docker logs -f --tail 100 web
docker exec -it web sh
docker inspect web | head -40
docker stats --no-stream

Why did it stop?

A container stops when its main process exits. Check the exit code and last logs.

docker ps -a
docker inspect -f "{{.State.ExitCode}}" web   # 0 = clean, 137 = killed/OOM, 1 = app error
docker logs web

Add a HEALTHCHECK to your Dockerfile or Compose service so Docker can mark a container unhealthy and orchestrators can restart it.

Quick check

Quick check: Which command opens a shell in a running container?

  • docker exec -it web sh
  • docker build -t web .
  • docker pull web
  • docker rm web
Answer

docker exec -it web sh — exec runs a command inside an existing container.