How to View Docker Container Logs?

In this tutorial, I will explain how to view logs in a running Docker container in a simple and easy-to-understand way. You will learn the importance of logs and various methods to access them, perfect for a beginner developer.

Logs are essential for monitoring container status, checking for errors, and debugging issues within your containerized applications. Docker containers can run in either detached mode or non-detached (foreground) mode. When a container is in detached mode, it runs in the background without occupying the terminal.

Now, let’s learn how to use the docker logs command to view the logs of a container running in detached mode.

Get Docker Container ID

Before you can get access to log entries, you will first need to find the container’s ID or name. To find both the container ID and name, use the docker ps command like this:

docker ps

This command will show you a list of all running containers with their IDs, names, and other details.

View Docker Container Logs

To see the logs of a container running in detached mode, you can use the docker logs command. This command allows you to view the logs of a running container without needing to attach to it. The logs can be useful for monitoring container status, checking for errors, or debugging issues.

Here’s how to use the docker logs command:

docker logs CONTAINER_ID_OR_NAME

Replace CONTAINER_ID_OR_NAME with the actual container ID or name. You can obtain the container ID from the output of the docker run -d command, or you can find both the container ID and name using the docker ps command.

For example, if your container’s name is ‘my-container-name’, you would run:

docker logs my-container-name

By default, the docker logs command shows the entire log history of the container. You can also use additional options to customize the log output.

Display Real-Time logs

To display real-time logs as they are generated, use the --follow or -f option:

docker logs -f CONTAINER_ID_OR_NAME

Display a specific number of lines only

To display a specific number of lines from the end of the logs, use the --tail option followed by the number of lines:

docker logs --tail 100 CONTAINER_ID_OR_NAME

Display Logs generated since a specific timestamp

To display logs generated since a specific timestamp or relative time, use the --since option followed by the timestamp or relative time (e.g., ‘5m’ for 5 minutes ago):

docker logs --since 5m CONTAINER_ID_OR_NAME

Keep in mind that the docker logs command works for containers running in both detached and non-detached (foreground) modes. Accessing logs is essential for monitoring and troubleshooting your containerized applications, so it’s a valuable command to know and use.

Filter logs by specific keywords or patterns

When working with Docker container logs, you might want to search for specific keywords or patterns within the logs to quickly find relevant information. You can use Unix command-line tools like grep to filter logs by specific keywords or patterns. Here’s how to do it.

Filter logs by a specific keyword

To filter logs by a specific keyword, use the docker logs command followed by a pipe (|) and grep, along with the keyword you want to search for. For example, to find log lines containing the word “error”, run:

docker logs CONTAINER_ID_OR_NAME | grep "error"

Replace CONTAINER_ID_OR_NAME with the actual container ID or name.

Filter logs using regular expressions

You can also filter logs using regular expressions. For example, to search for log lines containing either “error” or “warning”, run:

docker logs CONTAINER_ID_OR_NAME | grep -E "error|warning"

Case-insensitive search

To perform a case-insensitive search, add the -i option to the grep command:

docker logs CONTAINER_ID_OR_NAME | grep -i "Error"

This command will match log lines containing “Error”, “error”, “ERROR”, etc.

Filter logs based on multiple conditions

You can also chain multiple grep commands to filter logs based on multiple conditions. For example, to find log lines containing the word “error” but not “timeout”, run:

docker logs CONTAINER_ID_OR_NAME | grep "error" | grep -v "timeout"

 

Final Words

In this tutorial, I have explained the basics of viewing logs in a running Docker container. Here are some key takeaway points to remember:

  1. Logs are essential for monitoring container status, checking for errors, and debugging issues in your containerized applications.
  2. I showed you how to use the docker logs command to view logs of a running container without attaching to it, which is perfect for beginners.
  3. You’ve learned how to display real-time logs, limit the number of log lines shown, and view logs generated since a specific timestamp.
  4. The techniques you’ve learned in this tutorial apply to both detached and non-detached (foreground) Docker containers.
  5. As a beginner developer, understanding how to access and interpret logs is crucial for effectively troubleshooting and maintaining your containerized applications.

Keep exploring and enjoy your journey into the world of containerization!

To learn more about Docker, please check Docker Tutorials page.