首先我们查看容器的文件系统
docker container inspect mycontainer | jq '.[0].GraphDriver'
将会输出如下结果
{
"Data": {
"LowerDir": "/var/lib/docker/overlay2/63ec1a08b063c0226141a9071b5df7958880aae6be5dc9870a279a13ff7134ab-init/diff:/var/lib/docker/overlay2/524a0d000817a3c20c5d32b79c6153aea545ced8eed7b78ca25e0d74c97efc0d/diff",
"MergedDir": "/var/lib/docker/overlay2/63ec1a08b063c0226141a9071b5df7958880aae6be5dc9870a279a13ff7134ab/merged",
"UpperDir": "/var/lib/docker/overlay2/63ec1a08b063c0226141a9071b5df7958880aae6be5dc9870a279a13ff7134ab/diff",
"WorkDir": "/var/lib/docker/overlay2/63ec1a08b063c0226141a9071b5df7958880aae6be5dc9870a279a13ff7134ab/work" },
"Name": "overlay2"
}
可以清晰地看到 lowerdir、mergerdir 、workdir 以及 upperdir 。其中 upperdir 是可写层,MergedDir 是最终合并的结果,如果我们执行
ls /var/lib/docker/overlay2/63ec1a08b063c0226141a9071b5df7958880aae6be5dc9870a279a13ff7134ab/merged
bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
便可以看到合并过后的rootfs 根目录了。当然如果你在 upperdir 里面的改动也会体现在 mergeddir里面。我们可以测试一下
进入 upperdir
进入 upperdir
# cd /var/lib/docker/overlay2/d4632166ec8af2c274d8575bc60776489b9ca57631da96809ba533d0d065ce93/diff
创建文件
# cd root/ && touch testfile
查看mergedDir结果
# ls /var/lib/docker/overlay2/d4632166ec8af2c274d8575bc60776489b9ca57631da96809ba533d0d065ce93/merged/root/
testfile
执行mount 命令就可以清楚地看到底层目录是如何挂载到 merge dir 的。
overlay on/var/lib/docker/overlay2/d4632166ec8af2c274d8575bc60776489b9ca57631da96809ba533d0d065ce93/merged type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay2/l/BUMMKXX6V7NKDDXVVYFYMZPD7U:/var/lib/docker/overlay2/l/UAFOM6XM534QEAFJ3OH5NRLJU4,upperdir=/home/chenxy/docker/overlay2/d4632166ec8af2c274d8575bc60776489b9ca57631da96809ba533d0d065ce93/diff,workdir=/home/chenxy/docker/overlay2/d4632166ec8af2c274d8575bc60776489b9ca57631da96809ba533d0d065ce93/work)