Container Logs

DockerBeginner
Practice Now

Introduction

Applications running inside containers typically write their status and error messages to standard output and standard error. This is an important design pattern in containerized systems: instead of every container managing its own complex log files, the container runtime captures the stream and makes it available centrally.

This is one reason container images are often built differently from traditional server software. In container platforms, writing useful stdout/stderr output is often preferred over hiding logs only inside local files.

As a developer or system administrator, accessing these logs is crucial for understanding what is happening inside background containers. In real platforms such as Kubernetes, this stdout/stderr model is also the basis for centralized logging pipelines.

In this lab, you will learn how to:

  • Run a container that generates continuous output.
  • Fetch the logs that have been generated so far.
  • Follow the logs in real-time to monitor the application as it runs.
  • Safely stop monitoring without stopping the application.

Start a Background Process

In this step, you will start a Docker container in detached mode. To simulate a real application, we will run a simple script that prints a message every second.

When you run a container in detached mode (using the -d flag), it runs in the background, so you won't see its output immediately in your terminal.

The output still exists; it is just being captured by Docker instead of streaming directly into your current shell session.

Run the following command to start the container:

docker run -d --name web-log-app alpine sh -c "while true; do echo 'Application is running...'; sleep 1; done"

Breaking down the command:

  • docker run -d: Starts the container in the background.
  • --name web-log-app: Names the container "web-log-app".
  • alpine: Uses the lightweight "Alpine Linux" image.
  • sh -c "...": This is the command running inside the container. It executes a shell script that runs an infinite loop (while true), printing a message (echo) and pausing for 1 second (sleep 1) in each iteration.

Verify that the container is running:

docker ps

You should see web-log-app listed as an active container.

View Existing Logs

Since the container is running in the background, its output is hidden from your current terminal view. Docker captures this output and allows you to view it using the docker logs command.

In this step, you will fetch the logs that have been generated since the container started.

Run the following command:

docker logs web-log-app

You will see a list of "Application is running..." messages.

How it works:

The docker logs command retrieves all the logs stored up to this moment and prints them to your screen. It then exits immediately, returning you to the command prompt. This is useful for checking the history of an application or verifying its startup status.

So docker logs is historical output up to now, not an interactive shell into the container. It is a read-only view of what the container's main process has been writing.

If you run the command again after a few seconds, you will see a longer list because the container is still generating logs in the background.

docker logs web-log-app

Monitor Logs in Real-Time

Sometimes, simply seeing past logs isn't enough. You may need to watch the logs as they happen to debug an issue or monitor live traffic. This is similar to using the tail -f command in Linux.

In this step, you will use the -f (follow) flag to stream the logs in real-time.

Run the following command:

docker logs -f web-log-app

What is happening?

The command prompt does not return. Instead, your terminal is now "attached" to the container's log stream. You will see a new "Application is running..." message appear every second.

This is conceptually similar to tail -f, but the data source is Docker's captured log stream rather than a normal file you are reading directly.

This mode is incredibly useful for watching an application's behavior as you interact with it.

Detach and Clean Up

Your terminal is currently "stuck" following the logs. The container is still running, and the docker logs command is waiting for new output.

To stop watching the logs and return to your command prompt, you need to send an interrupt signal.

Press Ctrl+C on your keyboard.

Important Concept:

Pressing Ctrl+C here stops the docker logs command (the viewer), but it does not stop the container itself. The web-log-app container is still running in the background and generating logs.

That distinction matters operationally. Observing a container and controlling a container are different actions, and confusing them is a common beginner mistake.

You can verify this by checking the process list:

docker ps

You should still see web-log-app in the list.

Clean Up:

Now that we are done with the lab, let's stop and remove the container to free up resources. We will use docker rm -f which forces the removal of a running container (combining stop and remove actions).

docker rm -f web-log-app

Summary

In this lab, you have learned the fundamentals of accessing Docker container logs. You have practiced:

  • Running a background process: Using docker run -d to keep your terminal free.
  • Viewing existing logs: Using docker logs <container_name> to see past output.
  • Monitoring real-time logs: Using docker logs -f <container_name> to stream output as it happens.
  • Detaching: Using Ctrl+C to stop viewing logs while keeping the application running.

These skills are essential for debugging and monitoring containerized applications in any environment.