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.

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.

Frequently Asked Questions

  • How can I view logs for a stopped container?
    You can use the same docker logs command for stopped containers as well. Just replace the CONTAINER_ID_OR_NAME with the ID or name of the stopped container.
  • Can I filter logs by specific keywords or patterns?
    Yes, you can use Unix command-line tools like grep to filter logs. For example: docker logs CONTAINER_ID_OR_NAME | grep "error" will display log lines containing the word “error”.
  • How do I clear or truncate container logs?
    There isn’t a direct docker command for this, but you can truncate logs using the following command: echo "" > $(docker inspect --format='{{.LogPath}}' CONTAINER_ID_OR_NAME).
  • Where are the Docker container logs stored on the host?
    Docker stores container logs in JSON files located in the /var/lib/docker/containers/CONTAINER_ID/ directory, in a file named CONTAINER_ID-json.log.
  • Can I change the log driver or customize log settings for a container?
    Yes, you can specify a different log driver or customize log settings using the --log-driver and --log-opt options when creating a container with docker run. Refer to the Docker documentation for more details.
  • How can I view logs for all containers or multiple containers at once?
    Docker doesn’t provide a built-in command to do this, but you can use third-party tools like ctop or Portainer to view logs for multiple containers. Alternatively, you can use a script or loop to run docker logs for each container in a list.
  • How can I view logs for a container running as part of a Docker Compose setup?
    You can use the docker-compose logs command, followed by the service name defined in the docker-compose.yml file. For example, docker-compose logs my-service.