docker日志
首先我们先日常运行一个nginx容器,日志会在终端输出访问日志以及错误日志
[root@localhost ~]# docker run -it -p 80:80 nginx
现在我们打开网站访问一下nginx
因为我们在启动日志的时候没有使用-d参数,httpd容器以前台方式启动,日志会直接打印到当前的终端窗口。 加-d参数以后后台方式运行容器,我们就看不到输出的日志了
这种情况下如果要查看容器的日志,有两种方法 attach到该容器
[root@localhost ~]# docker attach 2ecf757 192.168.172.1 - - [20/Jun/2020:06:34:03 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36" "-" 192.168.172.1 - - [20/Jun/2020:06:34:03 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36" "-" 192.168.172.1 - - [20/Jun/2020:06:34:04 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36" "-"
用docker logs 命令查看日志
attach 的方法在实际使用中不太方便,因为:
1.只能看到attach之后的日志,以前的日志不可见
2.退出attach状态比较麻烦ctrl+p+q 一不小心很榕溪将容器杀掉,查看容器推荐的方法是 docker logs命令
[root@localhost ~]# docker logs -f 22fbc207a41a6d /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Configuration complete; ready for start up 192.168.172.1 - - [20/Jun/2020:06:39:55 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36" "-" 192.168.172.1 - - [20/Jun/2020:06:39:55 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36" "-" 192.168.172.1 - - [20/Jun/2020:06:39:55 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36" "-"
docker logs能够打印出容器启动以来完整的日志,并且 -f参数可以继续打印出新产生的日志效果上和linux tail -f一样
docker支持多种日志方案
将容器日志发送到stdout 和stderr是docker的默认日志行为,实际上 docker提供了多种日志机制帮助用户从运行的容器中提取日志信息,这些机制被称作logging driver
docker默认的logging driver是json-file
[root@localhost ~]# docker info |grep 'Logging Driver' Logging Driver: json-file
如果容器在启动时没有特别指明,就会使用这个默认的json-file 会将容器的日志保存在json文件中,docker 负责格式化其内容 并输出到stdout和stderr 我们可有再host 的容器目录中找到这个文件容器路径为
[root@localhost ~]# docker container inspect 22fbc207a41a |grep 'LogPath' "LogPath": "/var/lib/docker/containers/22fbc207a41a6dc8227dd705768a635a729fd778b57da7632f7cf92a6e8c091e/22fbc207a41a6dc8227dd705768a635a729fd778b57da7632f7cf92a6e8c091e-json.log",
none是disable容器日志功能
syslog和journald 是linux上的两种日志管理服务
awslogs splunk和gcplogs是第三方日志托管服务
gelf和fluentd是两种开源的日志管理方案
容器启动时可以通过 --log driver 指定使用的logging driver 如果要设置docker默认的 logging driver 需要修改 docker daemon的启动脚本,指定 --log-driver参数
[root@localhost ~]# docker info |grep 'Logging Driver' Logging Driver: none