Filter logs by time range
In addition to viewing all logs or the most recent ones, you can also filter container logs by a specific time range. This is useful when you are investigating events that occurred during a particular period.
The docker logs
command provides the --since
and --until
options to filter logs based on time. These options accept various time formats, including RFC3339 timestamps, Unix timestamps, or relative times (e.g., 10m
for 10 minutes, 1h
for 1 hour).
Let's try filtering logs. First, we need to generate some logs at different times. We can do this by sending curl
requests to the Nginx container again.
Open a new terminal tab and run the curl
command a few times with a short delay between them.
curl http://localhost:80
sleep 5
curl http://localhost:80
sleep 5
curl http://localhost:80
Now, let's view the logs from the my-nginx
container with timestamps to see the times of these requests.
docker logs -t my-nginx
Observe the timestamps of the log entries generated by the curl
commands.
To filter logs since a specific time, you can use the --since
option. For example, to view logs generated in the last 1 minute:
docker logs --since 1m my-nginx
This command will show all log entries from the my-nginx
container that were generated within the last minute.
You can also filter logs up to a specific time using the --until
option. For example, to view logs generated before 1 minute ago:
docker logs --until 1m my-nginx
This command will show all log entries from the my-nginx
container that were generated more than one minute ago.
You can combine --since
and --until
to view logs within a specific time window. For example, to view logs between 5 minutes ago and 1 minute ago:
docker logs --since 5m --until 1m my-nginx
Remember that the exact output will depend on when you executed the curl
commands and when you run the docker logs
commands.