C.W.K.
Stream
Lesson 05 of 05 · published

Inside a Running Container — exec, logs, inspect

~16 min · commands, debugging

Level 0Container Curious
0 XP0/36 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

The debugging trio

When something is wrong with a running container, three commands answer 90% of your questions.

docker logs — the first stop

Whatever the main process writes to stdout/stderr is captured. Always check logs first; most issues are visible there.

docker exec — get inside

docker exec runs a new process inside an already-running container. Different from docker run, which creates a fresh container. Use exec for poking around: ls, ps, cat /etc/..., or an interactive shell.

docker inspect — the metadata firehose

Returns a giant JSON document with everything Docker knows about the container: state, network, mounts, env vars, restart count, exit code. Use Go templates with --format to extract specific fields.

Code

The triage sequence·bash
# Step 1: Is it running?
docker ps -a | grep myapp

# Step 2: What did it say?
docker logs --tail 100 myapp
docker logs -f myapp           # follow in real time
docker logs --since 10m myapp  # last 10 minutes

# Step 3: Get inside
docker exec -it myapp /bin/sh   # alpine
docker exec -it myapp /bin/bash # debian/ubuntu base

# Step 4: Pull specific facts out
docker inspect --format '{{.State.Status}}' myapp
docker inspect --format '{{.State.ExitCode}}' myapp
docker inspect --format '{{.NetworkSettings.IPAddress}}' myapp
exec for one-off commands·bash
# Run a Django migration in an existing container
docker exec -it api python manage.py migrate

# Check Postgres health from the DB container itself
docker exec db pg_isready -U postgres

# Tail a log file inside the container
docker exec -it api tail -f /app/logs/app.log

External links

Exercise

Start a container that fails after 5 seconds (e.g., docker run --name flaky alpine sh -c 'echo hi; sleep 5; exit 1'). After it dies, use docker logs and docker inspect to recover the output and the exit code. Write down both commands.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.