是否可以安装码头影像,然后将其作为正常目录或挂载设备访问(RO)? [英] Is it possible to mount a docker image and then access it (RO) as a normal directory or mounted device?
问题描述
我想采用Docker的形象(让我们说ubuntu:最新的),并且做出如下一些:
> docker-image-mount ubuntu:最新的my_little_ubuntu
> cd my_little_ubuntu
> ls
/ usr / var / bin等
有可能吗? >
我刚刚调查了docker如何存储他们的图像的内部结构。
如果 aufs
存储驱动程序,则有以下目录布局(我假设该docker住在 / var / lib / docker
)。
-
/ var / lib / docker / aufs / diff
在这个目录中,docker存储每个图像layer的数据。它只是一个包含文件的目录,Docker挂载在容器根目录中。 -
/ var / lib / docker / aufs / layers
这个目录docker只存储文本文件。每个文件包含某些图像的图层ID列表。
所以Docker本身就是这样的:
DOCKER_AUFS_PATH =/ var / lib / docker / aufs /
DOCKER_AUFS_LAYERS =$ {DOCKER_AUFS_PATH} / layers /
DOCKER_AUFS_DIFF = $ {DOCKER_AUFS_PATH} / diff /
error(){echo$ @1>& 2; }
如果[-z$ {IMAGE}];
然后
错误图像未指定
exit 1
fi;
如果[-z$ {TARGET}];
然后
错误目标未指定
exit 1
fi;
BRANCH =br
同时读取LAYER; do
BRANCH + =:$ {DOCKER_AUFS_DIFF} / $ {LAYER} = ro + wh
done< $ {DOCKER_AUFS_LAYERS} / $ {IMAGE}
mount -t aufs -o$ {BRANCH}$ {IMAGE}$ {TARGET}
其中$ {IMAGE}是docker容器的ID,$ {TARGET}是主机文件系统中安装映像的目录。 / p>
要卸载它只需拨打:
umount cf39b476aeec4d2bd097945a14a147dc52e16bd88511ed931357a5cd6f6590de
正如我在上面的评论中提到的,这很大程度上取决于存储驱动程序(显然在docker版本上),所以我不能保证你会得到这个代码工作。
I would like to take a Docker image (let's say ubuntu:latest) and make something like:
> docker-image-mount ubuntu:latest my_little_ubuntu
> cd my_little_ubuntu
> ls
/usr /var /bin etc.
Is it possible somehow?
I've just investigated internal structure of how docker stores their images.
In case of aufs
storage driver there is following directory layout (I assume, that docker lives in /var/lib/docker
).
/var/lib/docker/aufs/diff
in this directory docker stores data of each image "layer". It is just a directory with files, which docker mounts in container root./var/lib/docker/aufs/layers
in this directory docker stores just text files. Each files contains list of layer ID's for certain image.
So docker itself does something like that:
DOCKER_AUFS_PATH="/var/lib/docker/aufs/"
DOCKER_AUFS_LAYERS="${DOCKER_AUFS_PATH}/layers/"
DOCKER_AUFS_DIFF="${DOCKER_AUFS_PATH}/diff/"
error() { echo "$@" 1>&2; }
if [ -z "${IMAGE}" ];
then
error "Image is not specified"
exit 1
fi;
if [ -z "${TARGET}" ];
then
error "Target is not specified"
exit 1
fi;
BRANCH="br"
while read LAYER; do
BRANCH+=":${DOCKER_AUFS_DIFF}/${LAYER}=ro+wh"
done < "${DOCKER_AUFS_LAYERS}/${IMAGE}"
mount -t aufs -o "${BRANCH}" "${IMAGE}" "${TARGET}"
Where ${IMAGE} is ID of docker container, and ${TARGET} is existed directory in host filesystem where to mount image.
To unmount it just call:
umount cf39b476aeec4d2bd097945a14a147dc52e16bd88511ed931357a5cd6f6590de
As I mentioned in comment above, this is heavily depends on storage driver (and obviously on docker version), so I could not give you any guarantee that you will get this code working.
这篇关于是否可以安装码头影像,然后将其作为正常目录或挂载设备访问(RO)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!