Docker Foundations·Ship the environment with the app

Debugging containers

Logs, exec, inspect and exit codes reveal what a container is actually doing.

Beginner 12 min read Lesson 5 of 300 Challenge included
What you will be able to do
  • Read stdout and stderr with docker logs.
  • Enter a running container with docker exec -it.
  • Check exit codes and config with docker inspect.
  • Debug connectivity by confirming containers share a network.

In plain English

docker logs prints the stdout and stderr of a container, which is usually where an application reports why it crashed. docker exec runs a new command inside an already running container, so you can open a shell with docker exec -it web sh and poke around. docker inspect returns a large JSON description of a container, including its exit code, mounts, environment and network settings. An exit code tells you how the process ended: 0 means clean success, a non-zero code usually signals an error, and 137 often means the process was killed, commonly by an out-of-memory kill. Networking and DNS problems are frequent, and they usually come down to containers being on different networks or a service name that the embedded DNS cannot resolve.

Step by step

  1. 1Stream a container log with docker logs -f web.
  2. 2Open a shell inside a running container with docker exec -it web sh.
  3. 3Inspect a container with docker inspect to read its exit code and settings.
  4. 4List all containers, including stopped ones, with docker ps -a.
  5. 5Confirm two services share a network with docker network ls and docker network inspect.
terminalTerminal
docker logs -f web
docker exec -it web sh
docker inspect web
docker ps -a
docker network ls

Common mistakes

  • Assuming a container is fine because it started is wrong; a crashed process may have already exited.
  • Reading docker ps only shows running containers, so stopped crashes stay invisible until you add -a.
  • Calling a service by name from a different network fails, because DNS resolution is scoped to the shared network.
  • Treating every non-zero exit code as the same error hides the difference between a crash and a kill.
Your turn

Read why a container stopped

Easy

List all containers including stopped ones, then inspect one to read its exit code.

BASH · starter
docker ps
Check your understanding before moving on.
Knowledge checkQuestion 1

Which command opens a shell inside an already running container?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Images and containers
Lesson 5 of 300 · 0% of course