How to use the command 'docker logs' (with examples)

How to use the command 'docker logs' (with examples)

The ‘docker logs’ command is used to print the logs of a container. It allows us to view the output produced by a running container or retrieve the logs of a stopped container. It is a useful command for troubleshooting issues, monitoring the container’s behavior, and analyzing any errors or warnings that might have occurred.

Use case 1: Print logs from a container

Code:

docker logs container_name

Motivation: The motivation for using this example is to print the logs of a specific container. It helps us to view the output and understand what happened inside the container.

Explanation:

  • docker logs: The command to print the logs of a container.
  • container_name: The name or ID of the container whose logs we want to print.

Example output:

2022-01-01T13:00:00.123456789Z This is a log message.
2022-01-01T13:00:01.987654321Z Another log message.

Use case 2: Print logs and follow them

Code:

docker logs -f container_name

Motivation: The motivation for using this example is to continuously stream and follow the logs of a container in real-time. It is useful for monitoring the container’s output and capturing any new log messages as they occur.

Explanation:

  • docker logs: The command to print the logs of a container.
  • -f: The option to follow the logs and continue printing new log messages as they are produced.
  • container_name: The name or ID of the container whose logs we want to print.

Example output:

2022-01-01T13:00:00.123456789Z This is a log message.
2022-01-01T13:00:01.987654321Z Another log message.
2022-01-01T13:00:02.345678912Z New log message.

Use case 3: Print last 5 lines

Code:

docker logs container_name --tail 5

Motivation: The motivation for using this example is to print only the last few lines of the container’s logs. It can be helpful when we want to quickly check the most recent log entries without viewing the entire log file.

Explanation:

  • docker logs: The command to print the logs of a container.
  • container_name: The name or ID of the container whose logs we want to print.
  • --tail 5: The option to specify the number of last log lines to display. In this case, we are displaying the last 5 lines.

Example output:

2022-01-01T13:00:01.987654321Z Another log message.
2022-01-01T13:00:02.345678912Z New log message.
2022-01-01T13:00:03.789012345Z Recent log message.
2022-01-01T13:00:04.567890123Z Another recent log message.
2022-01-01T13:00:05.432109876Z The latest log message.

Use case 4: Print logs and append them with timestamps

Code:

docker logs -t container_name

Motivation: The motivation for using this example is to include timestamps in the logs, making it easier to track when each log message occurred. This can be essential for monitoring the container’s behavior over time.

Explanation:

  • docker logs: The command to print the logs of a container.
  • -t: The option to add timestamps to each log message.
  • container_name: The name or ID of the container whose logs we want to print.

Example output:

2022-01-01T13:00:00.123456789Z This is a log message.
2022-01-01T13:00:01.987654321Z Another log message.

Use case 5: Print logs from a certain point in time of container execution

Code:

docker logs container_name --until 10s

Motivation: The motivation for using this example is to retrieve the logs from a specific point in the container’s execution. It can be useful when debugging an issue that occurred at a particular time or when looking for specific logs within a given timeframe.

Explanation:

  • docker logs: The command to print the logs of a container.
  • container_name: The name or ID of the container whose logs we want to print.
  • --until 10s: The option to specify a timestamp or duration until which to retrieve the logs. In this case, we are retrieving the logs until 10 seconds ago.

Example output:

2022-01-01T13:00:00.123456789Z This is a log message.
2022-01-01T13:00:01.987654321Z Another log message.

Conclusion:

The ‘docker logs’ command is a powerful tool for viewing and managing container logs. By understanding the various use cases and options available, we can effectively troubleshoot issues, monitor container behavior, and gain insights into the container’s execution.

Related Posts

How to use the command ansible-inventory (with examples)

How to use the command ansible-inventory (with examples)

The ansible-inventory command is used to display or dump an Ansible inventory.

Read More
How to use the command 'keep-header' (with examples)

How to use the command 'keep-header' (with examples)

The ‘keep-header’ command is a powerful tool in the tsv-utils package that allows users to manipulate files while keeping the first line intact.

Read More
How to use the command 'tcpdump' (with examples)

How to use the command 'tcpdump' (with examples)

Tcpdump is a powerful command-line packet analyzer tool used to capture network traffic on a network interface.

Read More