6.1 Key Features of the docker logs
Command
Container logging is an important part of managing and monitoring Docker apps. Logs allow you to keep track of container behavior, troubleshoot issues, and understand how apps inside containers work. In this lecture, we'll dive into how to use the docker logs
command to log containers.
Main features of the docker logs
command
The docker logs
command is used to retrieve and view container logs. It allows you to:
- View the standard output (stdout) and standard error (stderr) of the container.
- Filter and follow logs in real-time.
- Search and analyze logs for troubleshooting issues.
6.2 Using the docker logs Command
1. Viewing Container Logs
To view container logs, use the command:
docker logs <container_id_or_name>
Example:
docker logs my_container
This command will output all logs of the my_container
container.
2. Following Logs in Real Time
To follow logs in real time, use the -f
(or --follow
) flag:
docker logs -f <container_id_or_name>
Example:
docker logs -f my_container
This command will display new logs as they appear.
3. Limiting the Number of Output Lines
To limit the number of lines displayed by the docker logs
command, use the --tail
flag:
docker logs --tail <number_of_lines> <container_id_or_name>
Example:
docker logs --tail 10 my_container
This command will output the last 10 lines of logs from the my_container
container.
4. Displaying Logs Starting from a Specific Time
To display logs starting from a specific time, use the --since
flag:
docker logs --since <timestamp> <container_id_or_name>
Example:
docker logs --since 2022-01-01T00:00:00 my_container
This command will output logs from the my_container
container starting from January 1, 2022.
5. Displaying Logs Until a Specific Time
To display logs until a specific time, use the --until
flag:
docker logs --until <timestamp> <container_id_or_name>
Example:
docker logs --until 2022-01-02T00:00:00 my_container
This command will output logs from the my_container
container up to January 2, 2022.
6.3 Examples of Using docker logs
Example 1: Diagnosing Application Errors
If your application inside a container suddenly crashed with an error, you can use docker logs
to diagnose the cause:
docker logs my_app_container
Checking the logs can help you understand what went wrong and why the app crashed.
Example 2: Monitoring a Service
To monitor a web service in real-time, use the -f
flag:
docker logs -f web_service_container
This command lets you see all new logs being generated by the web service.
Example 3: Analyzing Logs for a Specific Timeframe
If you need to analyze logs for a specific timeframe, use the --since
and --until
flags:
docker logs --since "2023-07-01" --until "2023-07-02" my_app_container
This command outputs the logs of the my_app_container
for July 1, 2023.
6.4 Configuring Docker Logging Drivers
Docker supports various logging drivers that you can set up for containers. The main logging drivers include:
-
json-file
: Default. Logs are stored in files in JSON format. syslog
: Logs are sent to the system journal (syslog).journald
: Logs are sent to the journald system.-
gelf
: Logs are sent to servers supporting the GELF (Graylog Extended Log Format) protocol. fluentd
: Logs are sent to a Fluentd server.
Setting Up a Logging Driver
To set up a logging driver, use the --log-driver
flag when starting a container:
docker run --log-driver <driver_name> <container_image>
Example:
docker run --log-driver syslog my_app_image
This example starts a container using the syslog logging driver.
Configuring Logging Options
You can specify logging options with the --log-opt
flag:
docker run --log-driver syslog --log-opt syslog-address=udp://localhost:514 my_app_image
GO TO FULL VERSION