Skip to main content
Welcome to this comprehensive guide on logging mechanisms in containerized environments. In this article, we explore logging in Docker before transitioning to Kubernetes logging practices. This guide is ideal for developers and system administrators looking to efficiently manage logs and troubleshoot containerized applications.

Logging in Docker

Imagine a Docker container named “event simulator” that produces random events to mimic a web server. This container sends these event logs to its standard output. For instance, executing the container in the foreground using the command below:
may yield output similar to:
If you run the Docker container in detached mode using the -d option, the logs won’t display on the console immediately. Instead, you can inspect the logs later by using the docker logs command along with the container ID.
For example, starting the container in detached mode:
You can then view the logs with:

Logging in Kubernetes

Transitioning to Kubernetes, the process is similar but integrated into a managed cluster environment. You can create a pod using the same Docker image defined in a pod specification file. Once the pod is running, you can stream its logs with the kubectl logs command using the -f flag. Start by creating the pod:
Then, follow the logs using:
Below is the example pod definition file (event-simulator.yaml):
Executing the above commands will display log messages such as:

Multiple Containers in a Pod

Kubernetes allows you to create pods with multiple containers. Suppose you update your pod specification to include an additional container called “image-processor” as follows:
When a pod contains more than one container, running the command:
results in an error because Kubernetes cannot determine which container’s logs to display. To view logs for a specific container, explicitly specify the container name. For example, to view logs for the “event-simulator” container:
This command will then output logs similar to:

Conclusion

This guide has walked you through managing logs in both Docker and Kubernetes environments. Understanding these logging practices is critical for debugging and monitoring your applications. With these insights, you are now ready to move on to coding exercises that further reinforce your knowledge of logging in containerized environments. Thank you for reading, and happy logging!

Watch Video

Practice Lab