CodeGym /Courses /Docker SELF /Working with container logs

Working with container logs

Docker SELF
Level 11 , Lesson 3
Available

4.1 Basics of the docker logs command

The docker logs command is a handy tool for monitoring and debugging containers. Developers and sysadmins use it to view container logs, which helps diagnose problems and analyze app performance. In this lecture, we'll dive into how to use the docker logs command, what options are available, and how to effectively manage logs.

The docker logs command outputs logs for the specified container. These logs include all messages written by processes inside the container to the standard output (stdout) and error streams (stderr).

Syntax


docker logs [OPTIONS] CONTAINER

Where:

  • CONTAINER: name or ID of the container whose logs you want to view.
  • OPTIONS: additional parameters to customize log output.

Basic usage example

This command will show all accumulated logs of the my_container container.

Terminal


docker logs my_container

4.2 Options for the docker logs command

The docker logs command supports several options that let you customize the log output for specific needs.

1. Viewing logs in real-time

To view logs in real-time, use the -f (follow) option. This parameter allows you to follow log updates as they appear, similar to the tail -f command.

Terminal


docker logs -f my_container 

2. Limiting the number of output lines

You can limit the number of lines displayed by the docker logs command using the --tail option. This is useful if you only need the last few log lines, not the entire log.

Terminal


docker logs --tail 100 my_container 

This example will display only the last 100 lines of logs from the container my_container.

3. Viewing logs since a specific time

The --since parameter allows you to display logs starting from a specified time. Time can be provided in a timestamp format (e.g., 2023-07-20T15:00:00) or a relative format (e.g., 1h for logs from the last hour).

Terminal


docker logs --since "2023-07-20T15:00:00" my_container 

Or

Terminal


docker logs --since 1h my_container 

4. Viewing logs up to a specific time

The --until parameter allows you to display logs up until a specified time. The time format is the same as the --since parameter.

Terminal


docker logs --until "2023-07-20T16:00:00" my_container 

Or

Terminal


docker logs --until 10m my_container 

5. Combining stdout and stderr

By default, the docker logs command outputs both the standard output stream (stdout) and the standard error stream (stderr). You can specify which streams to output using the --stdout and --stderr options.

Terminal


docker logs --stdout my_container 

This example will display only messages from the standard output stream.

4.3 Examples of usage

Example 1: viewing the last lines of logs and following updates

This example will display the last 50 lines of logs from the container my_container and will follow new entries in real time.

Terminal


docker logs -f --tail 50 my_container 

Example 2: displaying logs for the last 24 hours

This example will display all logs accumulated in the last 24 hours for the container my_container.

Terminal


docker logs --since 24h my_container 

Example 3: displaying logs for a specific time period

This example will display logs recorded between 3:00 PM and 4:00 PM on July 20, 2023, for the container my_container.

Terminal


docker logs --since "2023-07-20T15:00:00" --until "2023-07-20T16:00:00" my_container 

4.4 Practical Scenarios

1. Debugging an application

If your app isn't working right or is "crashing," checking the logs can help figure out what's wrong. Use the docker logs command to find errors and warnings in the logs.

Terminal


docker logs my_app_container 

2. Monitoring server activity

To monitor server activity in real-time, you can use the docker logs command with the -f option.

Terminal


docker logs -f web_server_container 

This lets you track the server's requests and responses in real-time.

3. Performance analysis

If you want to analyze your app's performance during a specific time period, use the --since and --until flags.

Terminal

        
docker logs --since "2023-07-20T00:00:00" --until "2023-07-20T23:59:59" my_app_container 
3
Task
Docker SELF, level 11, lesson 3
Locked
Viewing container logs
Viewing container logs
3
Task
Docker SELF, level 11, lesson 3
Locked
Monitoring logs in real-time
Monitoring logs in real-time
3
Task
Docker SELF, level 11, lesson 3
Locked
Limiting the number of log lines
Limiting the number of log lines
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION