Docker Log use


foreword

Viewing docker logs usually uses the docker log command, and you can view the functions provided by the docker log by commanding docker help logs :
insert image description here

Options (optional parameters):
--details Display more detailed information
-f, --follow Track real-time logs --since string Display logs after
a certain (time) timestamp
, or relative time, such as 42m (ie 42 minutes) –tail string display N lines after the end of the log, the default is to display all -t, --timestamps add timestamp (timestamp)
before each line of log –until string display the log before a certain timestamp of the log, or relative time, such as 42m (ie 42 minutes)


1. Common commands of Docker logs

docker log provides the function of querying logs according to time or time period, and querying logs according to a certain keyword. The following will give examples of commonly used log viewing commands:

1. View the docker container:

docker ps: List container
OPTIONS description:
-a: Display all containers, including those that are not running.
-f : Filter the displayed content according to the condition.
--format : Specifies the template file for the return value.
-l : Show recently created containers.
-n : List the last n containers created.
--no-trunc : Do not truncate output.
-q : Quiet mode, only display the container number.
-s : Display the total file size.

2. View container logs (all):

docker logs -f container ID


3. View the container log, only the last 100 lines are displayed:

docker logs --tail 100 container ID

4. View the logs of the last 30 minutes of the container:

docker logs --since 30m container ID

5. View the logs of the container after a certain time:


The demonstration here is to query the logs docker logs --since 2023-03-03T10:13:58.655 container ID after 10:13:58 am on March 3, 2023

6. View the logs of the container for a certain period of time:

docker logs --since 2023-03-03T10:13:58.655 --until 2023-03-03T10:13:58.880 container id

7. View the container log and display the timestamp:

docker logs -t container ID

8. View the latest log of a certain time in the container log:

The demonstration here is the latest ten lines of log information within ten days
docker logs --tail 10 --since 240h container

9. Write container logs to a file:

The demonstration here is to write the last 100 logs of the specified container to the error file
docker logs --tail 100 container ID>>error.log

9. Query logs based on a certain keyword:

The demonstration here is to query logs according to the error keyword
docker logs container ID | grep error

10. Query the logs of the container for a certain period of time, and query based on keywords:

Here is a demonstration to view the logs of the container from 665 to 880 at 10:13:58 AM on March 3, 2023, and filter
docker logs according to the keyword 'Tomcat started on port' --since 2023-03-03T10:13:58.655 --until 2023-03-03T10:13:58.880 Container ID | grep 'Tomcat started on port'

Guess you like

Origin blog.csdn.net/qq_43600166/article/details/129350647