15

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?

fbrusch
  • 567
  • 2
  • 6
  • 7
  • This is definitely possible, because docker do it itself. But this is very dependent on particular storage driver (aufs or btrfs). I believe, answer is here https://github.com/docker/docker/blob/master/daemon/graphdriver/aufs/aufs.go. I'm interested in this questions and now gonna investigate it and write simple tool like docker-image-mount. – Viacheslav Kovalev Oct 26 '14 at 17:32

2 Answers2

8

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.

Viacheslav Kovalev
  • 1,745
  • 12
  • 17
6

You could use Podman and Buildah to mount a container image (RW).

prompt:~ # podman pull ubuntu

prompt:~ # container=`buildah from ubuntu`

prompt:~ # echo $container
ubuntu-working-container

prompt:~ # mnt=`buildah mount $container`

prompt:~ # echo $mnt
/var/lib/containers/storage/overlay/2b1...09b/merged

prompt:~ # ls $mnt
bin  boot  dev  etc  home  lib  ...  usr  var

prompt:~ # buildah umount $container

If you actually want to mount a running or stopped container, you could mount it using the podman mount command.

aventurin
  • 2,056
  • 4
  • 26
  • 30
  • Although it would make more sense to just do the latter... podman run image; podman ps -a; podman mount id – dagelf Feb 08 '21 at 07:10