How to view logs from a running Docker container

Learn how to view logs from a running Docker container using the docker logs [container_id] command. It shows stdout and stderr in real time or history, helping you debug, monitor behavior, and verify outputs. Other commands focus on config, not live log data.

Outline

  • Hook: A quick scene where you need to see what a running container is doing.
  • The core command: docker logs [container_id] and why it’s the go-to for container output.

  • How logs work: stdout and stderr, real-time viewing, and retrospective checks.

  • Handy options: following, time filters, and tailing to focus on the latest activity.

  • Quick contrasts: what other Docker commands do (and don’t) show—and why they aren’t log viewers.

  • A practical flow: how you’d actually pull logs during debugging.

  • Production mindset: logging drivers, rotation, and where logs land in real life.

  • Takeaway: the simple habit that saves you hours.

Docker logs: the real-time window into a running container

Let’s set the scene. You’ve started a microservice inside a container, and something’s not behaving as expected. Maybe you’re seeing a cascade of errors, maybe the app prints little progress messages. In those moments, the fastest way to see what’s happening inside is to peek at the container’s logs. The command you reach for is straightforward:

  • docker logs [container_id]

That’s it. No fanfare, just the raw output that the container’s process is sending to standard output and standard error. Replace [container_id] with the actual ID or name of your container, and you’re staring straight into the heart of the running process.

Why this command, and what makes it so handy?

Because containers aren’t just processes; they’re streams. They produce data, and that data travels through streams you can read. The docker logs command taps into those streams and surfaces them in your terminal. It’s a simple, sturdy bridge between your host and the isolated environment inside the container. When you’re debugging, this bridge is gold. You can see startup messages, configuration warnings, error traces, and even the occasional, honest-to-goodness success note that tells you the app is ready.

How to use docker logs like a pro

  • Real-time viewing: If you want to watch the logs as they’re created, add -f for “follow.” It’s like tail -f, but for Docker. You’ll see new lines appear as the application writes them.

  • Example: docker logs -f [container_id]

  • Time-bound checks: You can focus on logs from a certain window. The --since option is perfect for this. You can specify a timestamp, or a relative period like 1h, 30m, etc.

  • Example: docker logs --since 45m [container_id]

  • Tail the latest chatter: If you don’t need the whole history, just grab the last few lines with --tail.

  • Example: docker logs --tail 100 [container_id]

  • Combine it all: Follow while filtering back in time gives you a full, live story of what happened.

  • Example: docker logs -f --since 1h --tail 200 [container_id]

A gentle contrast: what other commands actually do

  • docker inspect [container_id]

  • Useful for discovering status, state, resource usage, and configuration, but it’s not a log viewer. It’s more like a diagnostic factsheet than a stream of ongoing output.

  • docker ps

  • Great for seeing which containers are running, their IDs, and basic status. It won’t show you application output.

  • docker view [container_id] and docker retrieve [container_id]

  • Not valid Docker commands. If you bump into them in a cheat sheet or a course slide, trust your instincts: they aren’t how you read logs.

A practical flow you can actually use

Here’s how a typical debugging session might unfold, in plain steps:

  1. Identify the container you care about. If you don’t know the ID, run docker ps to list running containers and note the name or ID.

  2. Check the current activity. docker logs [container_id] gives you a snapshot of what’s happened since the container started (or since logs were last rotated, depending on your setup).

  3. If the app is still running and you want to watch as it writes, start following: docker logs -f [container_id]. Keep your terminal ready for new lines.

  4. If you’re chasing a recent hiccup, grab only the last few minutes of output: docker logs --since 10m --tail 100 [container_id].

  5. When you’re done, you can exit the follow mode with Ctrl-C, and you’ll return to your shell with the latest state visible.

A few practical notes to avoid friction

  • Logs aren’t forever by default. Depending on your logging driver and the container’s lifecycle, the history could be limited. If you’re debugging a flaky issue, don’t assume the entire run is logged somewhere accessible. Consider configuring a logging driver that ships output to a central place or rotating files.

  • Real-world apps print a lot of noise. If you’re chasing a rare error, start with a broader window (--since) and then narrow down. It’s a little like paging back through a long chat: you skim for keywords, then read the surrounding lines.

  • Context matters. Sometimes the error message isn’t the whole story. Look for preceding lines that show startup steps, environment setup, or dependency checks. Often the root cause sits in the lines before the failure.

How this topic fits into the bigger Docker picture

Knowing how to read logs is a small skill with outsized impact. It complements other essentials you’ll see in Docker topics:

  • How containers start and stop, and what that means for lifecycle management.

  • The meaning of standard output vs standard error and how a program divides its messages.

  • The role of logging drivers, like json-file or syslog, and how they influence where your logs land.

  • Basic debugging workflows: combine logs with occasional inspections of container state, environment variables, and mounted volumes.

  • Simple troubleshooting patterns: reproduce locally, observe logs, adjust configuration, and re-run.

A quick mental model you can carry

Think of docker logs as the open window into a container’s day-to-day life. You don’t need to know every line of code inside the app to glean a lot from the output. Fresh messages right as they’re produced—followed or filtered by time—let you confirm that a service started correctly, that configuration was loaded as expected, or that a particular request path hit a snag. It’s not a fancy tool, but it’s incredibly practical.

Best practices you’ll appreciate in real work

  • Enable sensible logging in the app itself. Make sure important events produce clear, timestamped output.

  • Use a consistent logging strategy across services. If one container logs to stdout and another to a file, you’ll spend more time chasing logs than solving problems.

  • Consider centralized logging for production. Docker’s logging drivers can push logs to syslog, a central log server, or a cloud log service. A single pane of glass beats hunting through scattered files.

  • Rotate logs to avoid disk pressure. If you’re running containers locally or on a dev machine, the last thing you want is a giant log file chewing up space.

  • Tie logs to metrics when possible. If you’re monitoring health, correlate log events with service checks or error rates to spot recurring issues quickly.

The takeaway, plain and simple

When you’re staring down a running container and need to know what’s happening inside, docker logs [container_id] is your first friend. It’s the direct line to the app’s visible output—no fuss, just the essential signals you need to diagnose and understand behavior. By learning to follow, limit, and time-box those logs, you turn a messy debugging moment into a smooth, informed investigation.

If you’re exploring Docker for the long haul, keep this habit gentle and steady. Start with the raw logs, sprinkle in follow mode for live feedback, and then refine with time filters to zero in on the exact moment that matters. Before you know it, reading logs becomes second nature—an everyday tool in the toolbox that keeps your projects moving with confidence.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy