Debugging a Running Docker Container
Fast recall
Quick revision
Core answer
docker ps, docker logs, docker inspect, docker stats, and application health checks. Use docker exec -it sh only if the image contains a shell and policy permits it; minimal/distroless images may not. Inspect configuration carefully because environment output can contain secrets, and reproduce long investigations from the image/Dockerfile rather than changing a running container.Interview-ready guide
Interview answer
I debug a running container from the outside in. First I check docker ps -a for state, restart count, and exit status. Then I read a bounded log window with docker logs --tail 200 and inspect health, mounts, networks, published ports, and configured limits with docker inspect. docker stats shows live resource pressure.
If the image contains diagnostic tools and policy allows it, I can run docker exec -it and inspect processes or connectivity. I do not assume /bin/bash exists—distroless images may have no shell—and I avoid dumping environment values that can contain secrets.
For a crash-loop or already exited process, logs and inspect are usually more useful than exec. Any manual change inside a running container is temporary evidence, not the fix. I reproduce the issue in the Dockerfile/configuration, rebuild the image, and verify the replacement container.
Debugging a Running Docker Container
Direct Answer
Begin with docker ps, docker logs, docker inspect, docker stats, and application health checks. Use docker exec -it <container> sh only if the image contains a shell and policy permits it; minimal/distroless images may not. Inspect configuration carefully because environment output can contain secrets, and reproduce long investigations from the image/Dockerfile rather than changing a running container.
Code Example
// Step 1: Check application logs
docker logs --tail 100 -f order-api
// Step 2: Open shell inside running container
docker exec -it order-api /bin/bash
// Inside container: check if config file exists
ls -la /app/config/
// Inside container: verify environment variables
env | grep DATABASE
// Inside container: test network connectivity
curl http://localhost:8080/actuator/health
// Step 3: Inspect container configuration
docker inspect order-api
// Filter inspect output for specific info (environment variables)
docker inspect -f '{{.Config.Env}}' order-api
// Monitor real-time resource usage
docker stats order-api
// See running processes inside container
docker top order-api
Common Mistakes & Pitfalls
- Only using docker logs: Freshers stop at logs and give up when the error isn't obvious. They don't realize they can open a shell with docker exec and poke around inside the container to check files, test commands, or verify configuration.
- Forgetting -it flags on docker exec: Running docker exec without -i -t results in non-interactive or missing terminal output. The -it combination is essential — -i keeps stdin open, -t allocates a pseudo-TTY for proper terminal behavior.
- Not having debugging tools in the image: Opening a shell with docker exec only to find curl, wget, and even ls are missing because the base image is Alpine or a distroless variant. Production images strip unnecessary tools, but during debugging you need them — either use a different base image for dev or install tools temporarily.
Check your understanding
Answer these without looking back at the article.
- How do you debug a container that crashes immediately on startup?
- What is docker inspect and what information does it show?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.