码头工人在哪里保存已停止的容器生成的文件?

时间:2019-01-26 23:53:55

标签: docker

我创建了一个没有-v的容器,在其上保存了一个文本文件,关闭了该容器,再次打开该文件后,该文件仍然存在。问题是:如何? Docker在主机上的何处存储此文件?

复制步骤:

docker run -dti --name alpine alpine sh
docker exec alpine sh -c 'echo "Hello World!!!" > hello.txt'
docker exec alpine sh -c 'ls'
docker stop alpine
docker start -ai alpine
ls

1 个答案:

答案 0 :(得分:2)

在创建不带有--read-only标志的容器时,会在映像顶部(在这种情况下为Alpine)上创建可写文件系统层,以便该容器文件系统可写。除非您删除容器,否则该容器将一直存在。

主机系统上的位置取决于Docker的数据根(默认值:/var/lib/docker)和所使用的存储驱动程序(可能为overlay2)。您可以在GraphDriver属性中找到用于您的容器和数据目录的存储驱动程序。

docker container inspect --format '{{ .GraphDriver }}' alpine

"GraphDriver": {
    "Data": {
        "LowerDir": "/var/lib/docker/overlay2/d71e8f30d831f1c72e883e8de3c7560b2a69751f942fe8d5e24b740699872953-init/diff:/var/lib/docker/overlay2/31f665ee64e368e98d39a709dd8662bcf92726be0c992ccad07553e277fe754f/diff:/var/lib/docker/overlay2/a19268faed5bf38f3ad574274c2062bc29fad9406938b63e50451eeba440b30b/diff:/var/lib/docker/overlay2/52b4b93b90556b85eb8b8680e037b9ba5eaaedaf91a197b5270ab25d85cc43c1/diff:/var/lib/docker/overlay2/27cbda99661de508ba2fd6aefe3b5f98a3ef75966742f6566b7313065f713da0/diff:/var/lib/docker/overlay2/8f5c1f89ef130b85016f114dc5e7ccc410cc2a0def823dc65bf9dad15f76eb47/diff:/var/lib/docker/overlay2/483646cdc089e9828767c8ce6895da362a56ea7fd35881f70aee996d71dbf70a/diff:/var/lib/docker/overlay2/ebbdc64049110d1710755176a2fb061759e25c52b3f90da65e6ae2fbfd7dc4dc/diff:/var/lib/docker/overlay2/756e3bb714cce5892ce43d59d11492a7b027a0fe37abae2d5770cebe59a70bc3/diff:/var/lib/docker/overlay2/362674379f2d583a5572c1797aed2ff03eefa786a509e04913cfb94a16a774dd/diff",
        "MergedDir": "/var/lib/docker/overlay2/d71e8f30d831f1c72e883e8de3c7560b2a69751f942fe8d5e24b740699872953/merged",
        "UpperDir": "/var/lib/docker/overlay2/d71e8f30d831f1c72e883e8de3c7560b2a69751f942fe8d5e24b740699872953/diff",
        "WorkDir": "/var/lib/docker/overlay2/d71e8f30d831f1c72e883e8de3c7560b2a69751f942fe8d5e24b740699872953/work"
    },
    "Name": "overlay2"
}

UpperDir仅包含您所做的更改,包括示例中的hello.txt文件。您可以执行以下操作来导航到它。

cd $(docker container inspect --format '{{  .GraphDriver.Data.UpperDir }}' alpine)