Debug Containers with Logs and Inspect
Overview
When a container fails or behaves unexpectedly, use Docker's observation commands before changing the image. Logs describe application output, while inspect describes container configuration. These two sources often identify whether the bug is application code or runtime setup.
Detailed Notes
Read logs
docker logs my-nginx
docker logs --tail 50 my-nginx
docker logs --follow my-nginx
The --follow option streams new output until you press Ctrl+C. It does not change or restart the container.
If a container exits:
docker ps -a
docker logs CONTAINER_NAME
docker inspect CONTAINER_NAME --format '{{.State.ExitCode}}'
An exit code of 0 usually means the process intentionally finished. A non-zero code means the process reported an error or was terminated.
Execute a command inside a running container
docker exec -it my-nginx sh
execstarts a new process inside the running container.-ikeeps standard input open.-tcreates an interactive terminal.shstarts the shell.
Inside the shell, useful commands may include ps, cat, env, ls or wget, depending on what the image contains. Exit with exit.
Inspect configuration
docker inspect my-nginx
docker inspect --format '{{json .NetworkSettings.Ports}}' my-nginx
docker inspect --format '{{json .Mounts}}' my-nginx
When the output is long, use --format to extract only relevant values.
Copy files for diagnosis
docker cp my-nginx:/etc/nginx/nginx.conf ./nginx.conf
docker cp ./debug.txt my-nginx:/tmp/debug.txt
Copying is diagnostic. Changes made with docker cp remain in the container's writable layer and do not modify the source image.
Common Interview Questions
Finished this lesson?
0 of 27 lessons done · 0% of the course

