SkillSprout
Chapter 1 · Lesson 6
Intermediate
1 min read

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

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

bash
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

bash
docker exec -it my-nginx sh
  • exec starts a new process inside the running container.
  • -i keeps standard input open.
  • -t creates an interactive terminal.
  • sh starts 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

bash
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

bash
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