Skip to content

Practice Exercise 4: Exploring Docker Logs

Objective

Learn how to use the docker logs command to view and manage logs from Docker containers. You will also learn how to save those docker logs in a file.

Prerequisites:

  • Basic knowledge of Docker and container concepts.
  • Docker installed on your system.

Duration: 60 minutes

Tasks

Task 1: Run a Container

  • Start a new Docker container running a simple application. You can use an image like "nginx" for this exercise.

Task 2: Access Container Logs 1. Use the docker logs command to view the logs of the running container. The command format is as follows:

    docker logs <container_name_or_id>
  1. Record any logs you see, and make note of any information or messages.

Task 3: Filtering Container Logs

  1. Filter the logs based on a regular expression:

    docker logs <container_name_or_id> | grep 'error'
    
  2. Filter by Date and Time

    To filter logs by date and time, you can use --since and --until options with the docker logs command:

    docker logs --since "2023-11-01T00:00:00" --until "2023-11-14T00:00:00" <container_name_or_id>
    

Task 4: Tailing Container Logs

  1. Continuously monitor the logs of a running container:

    docker logs -f <container-id>
    
  2. Use the -t flag to display timestamps for each log entry:

    docker logs -ft <container-id>
    

Task 5: Saving Container Logs

  1. Save the logs of a container to a file

    docker logs <container-id> > container_logs.txt
    
  2. If you want to continuously save logs, you can use the docker logs command with the -f flag. This is useful for real-time monitoring.

    docker logs -f mynginx > path/to/save/logs/nginx_logs_continuous.txt
    

    Press Ctrl + C to stop the continuous log saving.

Task 6: View Logs of Multiple Containers

  • Create and run multiple containers with different names.
  • Use the docker logs command to view the logs of each container.
  • Compare the logs from different containers and note any differences.

Conclusion

In this lab exercise, you've gained practical experience with the docker logs command. You've learned how to access and manage logs from Docker containers, which is a crucial skill for troubleshooting and monitoring containerized applications.