Sub topic 2
Looking Inside a Running Container with `docker exec`
Assume you know docker run, Dockerfiles, Linux processes, and basic shell commands. A container is a process with isolated files, networking, and process namespaces. Debugging means comparing its expected configuration with what is actually running.
Entering a Running Container
docker exec runs a new command inside an existing, running container. It does not start a stopped container.
docker exec -it web sh
docker exec web env
docker exec web ps auxUse -i for interactive input and -t for a terminal. If the image includes Bash, use bash instead of sh. Check environment variables, mounted paths, processes, and local connectivity from inside the same network namespace.
Reproducing a Failed Start
A container that exits immediately cannot accept docker exec. Start a temporary container from the same image, but replace its normal entrypoint with a shell:
docker run --rm -it --entrypoint sh myimage:latestYou can then inspect files and variables, or run the original command manually. This often reveals a missing file, invalid setting, or permission error.
Detecting Memory Pressure
Exit code 137 commonly means the process received SIGKILL, often because the kernel's out-of-memory killer stopped it. Confirm rather than guessing:
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}}' web
docker stats --no-streamOOMKilled=true identifies a container memory kill. docker stats shows current usage, but it cannot reconstruct past usage.
Restart Loops
A restart policy can repeatedly start a process that immediately fails. Inspect the restart count and read logs from the previous attempts:
docker logs --tail 100 web
docker events --filter container=web --filter event=dieTemporarily remove or limit automatic restarts while investigating, so the failure remains observable.
Health Checks
A health check tests readiness separately from whether the main process is alive.
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1A container can be running but unhealthy when this probe fails. Inspect the probe output:
docker inspect --format '{{json .State.Health}}' webExercise
A container is running, but its health status is unhealthy. What should you inspect first?
Show answer
Inspect .State.Health and its recent probe output, then test the endpoint from inside the container or its network namespace.