如下,我们启动一个MySQL容器服务测试
docker pull daocloud.io/library/mysql:5.7.26
dir=/home/mysql-data && mkdir -p $dir
docker run -d --restart always --name mysql-source \
-v $dir:/var/lib/mysql \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=123456 \
daocloud.io/library/mysql:5.7.26
我们观察下Docker内部的一个磁盘挂载文件
[root@ys92.sae ~]# docker ps -a --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac16ea5ef6a3e02bb2ad0d4aa20fd48831a7f74894008fef5034024ccc3d1af4 daocloud.io/library/mysql:5.7.26 "docker-entrypoint.sh mysqld" 59 seconds ago Up 57 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-source
[root@ys92.sae ~]# df -h | grep ac16ea5ef6a3e02bb2ad0d4aa20fd48831a7f74894008fef5034024ccc3d1af4
shm 64M 0 64M 0% /var/lib/docker/containers/ac16ea5ef6a3e02bb2ad0d4aa20fd48831a7f74894008fef5034024ccc3d1af4/mounts/shm
/var/lib/docker/containers/ac16ea5ef6a3e02bb2ad0d4aa20fd48831a7f74894008fef5034024ccc3d1af4/ac16ea5ef6a3e02bb2ad0d4aa20fd48831a7f74894008fef5034024ccc3d1af4-json.log
即/var/lib/docker/containers/${CONTAINER}/${CONTAINER}-json.log
为容器内部的日志文件,该文件内容比docker logs -f ${CONTAINER}
还要多些
Docker支持多种日志记录方法,默认是json-file
,本文以限制json-file
方式的日志为例:
1 查看当前Docker Daemon日志记录方式
docker info --format '{{.LoggingDriver}}'
2 查看某个具体容器的日志记录方式
docker inspect -f '{{.HostConfig.LogConfig.Type}}' <CONTAINER>
3 修改整个Docker Daemon的日志记录规则(需要重启Docker)
touch /etc/docker/daemon.json
cat /etc/docker/daemon.json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
4 修改单个容器的日志记录方式
docker run -it --log-opt max-size=10m --log-opt max-file=3 alpine ash
5 相关参数含义解释
--log-opt max-size=10m,单个日志文件最大大小,可用单位有k、m、g,默认-1表示不限制
--log-opt max-file=3,最多保留几个日志文件,需配合上述参数使用,默认1
https://docs.docker.com/config/containers/logging/
https://docs.docker.com/config/containers/logging/json-file/
https://docs.docker.com/config/containers/logging/dual-logging/
https://blog.csdn.net/xys2015/article/details/110958107
csdn 111187189