Docker logs and docker exec: Debug Containers on Linux (Compose Included)

docker logs command - custom-dkrlog-featured.png

When a container exits or a Compose stack “works on my machine,” operators search docker logs first. Logs show application stderr; docker exec lets you inspect files and run one-off commands without rebuilding the image.

Setup: Install Docker,
Docker Compose,
Docker cleanup script.

Three Commands for Incidents

docker ps -a for state, docker logs for output, docker exec for live inspection.

logs exec inspect
workflow

docker logs Options

Tail recent lines, follow live output, or limit to the last hour during spikes.

bashlogs
docker logs --tail 100 mycontainer
docker logs -f --since 1h mycontainer
--tail -f
–since

Read Container State

Exit code 1 with no logs often means the entrypoint failed before logging started.

bashstatus
docker ps -a --format 'table {{.Names}}	{{.Status}}	{{.Ports}}'
docker ps -a
Exited

docker exec Into a Running Container

Get a shell as root or as the app user depending on image.

bashshell
docker exec -it mycontainer sh   # or bash if present
exec -it
debug

inspect for OOM and Restarts

docker inspect shows restart count and whether the kernel OOM-killed the container.

bashinspect
docker inspect mycontainer --format '{{.State.OOMKilled}} {{.RestartCount}}'
OOMKilled
RestartCount

docker compose logs

Filter by service name when only one tier misbehaves.

bashcompose
docker compose logs -f --tail=50 web
compose logs
-f web

Close the Loop

Fix config or env, recreate container, confirm healthy logs.

redeploy
verify

Quick Reference

  • docker logs -f --tail 100 NAME
  • docker exec -it NAME sh for live debug
  • Compose: docker compose logs -f SERVICE

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.