# Inspecting & Debugging Containers — Docker Fundamentals: Images, Containers and Compose

Source: https://www.geekswithgeeks.com/en/docker/docker-debugging

> 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.

```bash
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.

```bash
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

**Quiz:** Which command opens a shell in a running container?

- [x] 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.
