docker logs 探索使用

一、docker logs 命令详解

$ docker logs --help

Usage:	docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps,显示时间戳
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
  --tail -n :仅列出最新N条容器日志,从日志末尾显示的行数,默认为all

二、docker logs常见使用

2.1、显示时间戳

$ docker logs -t 17ea44912c8944234
2022-12-09T03:16:35.201515181Z + uname -a
2022-12-09T03:16:35.202190643Z Linux 17ea44912c89 4.19.91-009.ali4000.alios7.x86_64 #1 SMP Mon Jan 25 10:47:38 CST 2021 x86_64 x86_64 x86_64 GNU/Linux
2022-12-09T03:16:35.202352235Z + date
2022-12-09T03:16:35.203494991Z Fri Dec  9 11:16:35 CST 2022
2022-12-09T03:16:35.208878514Z + n=0
2022-12-09T03:16:35.208889880Z + for n in '{0..10}'
2022-12-09T03:16:35.208892789Z + echo 0
2022-12-09T03:16:35.208895175Z 0
2022-12-09T03:16:35.208897527Z + sleep 3
2022-12-09T03:16:38.204958294Z + n=0+1
2022-12-09T03:16:38.204998234Z + for n in '{0..10}'
2022-12-09T03:16:38.205001899Z + echo 1
2022-12-09T03:16:38.205004410Z 1
2022-12-09T03:16:38.205006735Z + sleep 3
2022-12-09T03:16:41.206244407Z + n=1+1
2022-12-09T03:16:41.206285611Z + for n in '{0..10}'
2022-12-09T03:16:41.206290410Z + echo 2
2022-12-09T03:16:41.206293964Z 2
2022-12-09T03:16:41.206297777Z + sleep 3
2022-12-09T03:16:44.207746341Z + n=2+1
2022-12-09T03:16:44.207787650Z + for n in '{0..10}'
2022-12-09T03:16:44.207791141Z + echo 3
2022-12-09T03:16:44.207793545Z 3
2022-12-09T03:16:44.207795763Z + sleep 3
2022-12-09T03:16:47.209048046Z + n=3+1

2.2、实时显示

$ docker logs -f 17ea44912c8944234
+ uname -a
Linux 17ea44912c89 4.19.91-009.ali4000.alios7.x86_64 #1 SMP Mon Jan 25 10:47:38 CST 2021 x86_64 x86_64 x86_64 GNU/Linux
+ date
Fri Dec  9 11:16:35 CST 2022
+ n=0
+ for n in '{0..10}'
+ echo 0
0
+ sleep 3
+ n=0+1
+ for n in '{0..10}'
+ echo 1
1
+ sleep 3
+ n=1+1
+ for n in '{0..10}'
+ echo 2
2

2.3、实时显示最近几行

$ docker logs --tail 5 17ea44912c8944234
10
+ sleep 3
+ n=10+1
+ date
Fri Dec  9 11:17:08 CST 2022

2.4、显示某时间之前的

$ docker logs -t  --until='2022-12-09T03:16:38.205004410Z' 17ea44912c8944234
2022-12-09T03:16:35.201515181Z + uname -a
2022-12-09T03:16:35.202190643Z Linux 17ea44912c89 4.19.91-009.ali4000.alios7.x86_64 #1 SMP Mon Jan 25 10:47:38 CST 2021 x86_64 x86_64 x86_64 GNU/Linux
2022-12-09T03:16:35.202352235Z + date
2022-12-09T03:16:35.203494991Z Fri Dec  9 11:16:35 CST 2022
2022-12-09T03:16:35.208878514Z + n=0
2022-12-09T03:16:35.208889880Z + for n in '{0..10}'
2022-12-09T03:16:35.208892789Z + echo 0
2022-12-09T03:16:35.208895175Z 0
2022-12-09T03:16:35.208897527Z + sleep 3
2022-12-09T03:16:38.204958294Z + n=0+1
2022-12-09T03:16:38.204998234Z + for n in '{0..10}'
2022-12-09T03:16:38.205001899Z + echo 1
2022-12-09T03:16:38.205004410Z 1

2.5、显示某时间之后的

$ docker logs -t  --since='2022-12-09T03:16:59.214551945Z' 17ea44912c8944234
2022-12-09T03:16:59.214551945Z + echo 8
2022-12-09T03:16:59.214554849Z 8
2022-12-09T03:16:59.214557686Z + sleep 3
2022-12-09T03:17:02.215835673Z + n=8+1
2022-12-09T03:17:02.215896527Z + for n in '{0..10}'
2022-12-09T03:17:02.215903772Z + echo 9
2022-12-09T03:17:02.215908185Z 9
2022-12-09T03:17:02.215911521Z + sleep 3
2022-12-09T03:17:05.217158016Z + n=9+1
2022-12-09T03:17:05.217212511Z + for n in '{0..10}'
2022-12-09T03:17:05.217217356Z + echo 10
2022-12-09T03:17:05.217220182Z 10
2022-12-09T03:17:05.217222920Z + sleep 3
2022-12-09T03:17:08.218451461Z + n=10+1
2022-12-09T03:17:08.218503831Z + date
2022-12-09T03:17:08.219270784Z Fri Dec  9 11:17:08 CST 2022

2.6、日志写入文件

$ docker logs -t  --since='2022-12-09T03:16:59.214551945Z' 17ea44912c8944234 >>/home/test.log

$ cat /home/test.log 
2022-12-09T03:16:59.214551945Z + echo 8
2022-12-09T03:16:59.214554849Z 8
2022-12-09T03:16:59.214557686Z + sleep 3
2022-12-09T03:17:02.215835673Z + n=8+1
2022-12-09T03:17:02.215896527Z + for n in '{0..10}'
2022-12-09T03:17:02.215903772Z + echo 9
2022-12-09T03:17:02.215908185Z 9
2022-12-09T03:17:02.215911521Z + sleep 3
2022-12-09T03:17:05.217158016Z + n=9+1
2022-12-09T03:17:05.217212511Z + for n in '{0..10}'
2022-12-09T03:17:05.217217356Z + echo 10
2022-12-09T03:17:05.217220182Z 10
2022-12-09T03:17:05.217222920Z + sleep 3
2022-12-09T03:17:08.218451461Z + n=10+1
2022-12-09T03:17:08.218503831Z + date
2022-12-09T03:17:08.219270784Z Fri Dec  9 11:17:08 CST 2022

三、如何实现日志查阅

3.1、docker run -v --entrypoint

使用 -v 将需要执行的脚本挂载到容器中, --entrypoint 执行此脚本

docker run -itd -v /home/test-dir/test-log.sh:/home/test-log.sh --entrypoint /home/test-log.sh centos:centos

3.2、制作镜像写入到DockerfileENTRYPOINT

ENTRYPOINT /home/test-log.sh

四、参考文档:

1、http://devops.weiminginfo.com/security/1176.html
2、https://blog.csdn.net/liuxiao723846/article/details/121864929
3、https://www.cnblogs.com/cuiyongchao007/p/14237106.html

你可能感兴趣的:(#,容器运维,docker,运维,linux,docker,logs,entrypoint)