在linux上,Docker的容器运行日志文件一般为/var/lib/docker/containers/container_id/container_id-json.log结尾的文件。可以通过下述方式查看所有容器的日志文件及其详细信息。
sudo find /var/lib/docker/containers/ -name *-json.log |xargs sudo ls -l
Docker logs 命令语法及参数
docker logs [OPTIONS] CONTAINER
参数说明:
Options:--details 显示更多的信息-f, --follow 跟踪实时日志--since string 显示自某个timestamp之后的日志,或相对时间,如30m(即30分钟)--tail string 仅列出最新的N条容器日志, 默认是all-t, --timestamps显示时间戳--until string 显示自某个timestamp之前的日志,或相对时间,如30m(即30分钟)
例子
例子1,输出容器日志:
docker logs -f nginx
上面的命令会向标准输出(屏幕)输出所有内容,不太好用。博主曾一直想当然认为 logs -f 跟 tail -f 类似,只是输出最近十条日志及新追加的日志,其实不一样。
可以加上–tail参数。
例子2,输出容器的最近50条记录:
# 只输出最近50条日志docker logs -f --tail=50 mynginx
例子3,查看最近30分钟的日志:
# 只输出最近10条日志docker logs -f --since 30m nginx
例子4,输出时间戳:
# 只输出最近10条日志docker logs -tf --tail=10 nginx
下面是输出例子
-03-13T05:14:03.979528647Zreturn self.get_template(template_name_or_list, parent, globals)-03-13T05:14:03.979531076Z File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 883, in get_template-03-13T05:14:03.979533595Zreturn self._load_template(name, self.make_globals(globals))-03-13T05:14:03.979536041Z File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 857, in _load_template-03-13T05:14:03.979538578Ztemplate = self.loader.load(self, name, globals)-03-13T05:14:03.979540973Z File "/usr/local/lib/python3.6/site-packages/jinja2/loaders.py", line 117, in load-03-13T05:14:03.979543446Zsource, filename, uptodate = self.get_source(environment, name)-03-13T05:14:03.979545891Z File "/usr/local/lib/python3.6/site-packages/flask/templating.py", line 60, in get_source-03-13T05:14:03.979548394Zreturn self._get_source_fast(environment, template)-03-13T05:14:03.979550776Z File "/usr/local/lib/python3.6/site-packages/flask/templating.py", line 89, in _get_source_fast-03-13T05:14:03.979553290Zraise TemplateNotFound(template)-03-13T05:14:03.979555690Z jinja2.exceptions.TemplateNotFound: 404.html
下面是直接用tail -f来跟踪日志文件的效果。没有上面的看起来可读性好。
tail -f /var/lib/docker/containers/379b7d4f0a62dacd4f9b0b85bf0f5be5ef84b1dfece3683a31fb83d3d9221ffe/379b7d4f0a62dacd4f9b0b85bf0f5be5ef84b1dfece3683a31fb83d3d9221ffe-json.log{"log":" return self._load_template(name, self.make_globals(globals))\n","stream":"stderr","time":"-03-13T04:28:43.632851693Z"}{"log":" File \"/usr/local/lib/python3.6/site-packages/jinja2/environment.py\", line 857, in _load_template\n","stream":"stderr","time":"-03-13T04:28:43.632866023Z"}{"log":" template = self.loader.load(self, name, globals)\n","stream":"stderr","time":"-03-13T04:28:43.632898144Z"}{"log":" File \"/usr/local/lib/python3.6/site-packages/jinja2/loaders.py\", line 117, in load\n","stream":"stderr","time":"-03-13T04:28:43.632910057Z"}{"log":" source, filename, uptodate = self.get_source(environment, name)\n","stream":"stderr","time":"-03-13T04:28:43.632933335Z"}{"log":" File \"/usr/local/lib/python3.6/site-packages/flask/templating.py\", line 60, in get_source\n","stream":"stderr","time":"-03-13T04:28:43.632942477Z"}{"log":" return self._get_source_fast(environment, template)\n","stream":"stderr","time":"-03-13T04:28:43.6329745Z"}{"log":" File \"/usr/local/lib/python3.6/site-packages/flask/templating.py\", line 89, in _get_source_fast\n","stream":"stderr","time":"-03-13T04:28:43.632983731Z"}{"log":" raise TemplateNotFound(template)\n","stream":"stderr","time":"-03-13T04:28:43.633006442Z"}{"log":"jinja2.exceptions.TemplateNotFound: 404.html\n","stream":"stderr","time":"-03-13T04:28:43.633017189Z"}