Accessing Docker Container File system from Mac OS host

I have been receiving queries from folks who have embraced the Docker for Mac lately. They would like to access the location on the Docker host where a Container file system is located. According to Docker :-

A container’s thin writable layer is stored in a directory under /var/lib/docker/aufs/diff/. With Docker 1.10 and higher, container IDs no longer correspond to directory names.

However, if you are using Native Docker for MacOS (not the one with Virtual Box and Boot2Docker image) this would be even more interesting.

  1. To access the file system of a particular Container, first, let us get the Container ID using the inspect command on the Docker Host.

    docker inspect --format <Container Name>

  2. Use the Alpine Docker image and mount your Host file system to the container

    docker run --rm -it -v /:/vm-root alpine:edge sh

    We need the ID of this container. So, you could combine the step 1 and 2 with the following

    docker run --rm -it -e CONTAINER_ID=$(docker inspect --format <Container Name>) -v /:/vm-root alpine:edge sh

    Now we have the CONTAINER_ID set as an environment variable in the alpine container.

  3. Once you are in the alpine container, you can visit the following directory

    cd /vm-root/var/lib/docker

  4. Inside this directory, you will be able to access all the familiar files that you are used to when administering Docker

  5. Now, we need to find the mount-id for the selected container to access the file system directories. We will use the CONTAINER_ID environment variable obtained in step 2. I have AUFS as my file system driver for the this example. To do that, use the following command.

    MOUNT_ID=$(cat /vm-root/var/lib/docker/image/aufs/layerdb/mounts/$CONTAINER_ID/mount-id)

  6. The above step will give you the mount-id. Now you can access the file system of the container under mnt directory with the mount-id

    ls -ltr /vm-root/var/lib/docker/aufs/mnt/$MOUNT_ID

This lists the entire root file system of the container

Enjoy !!!

P.S: This has been tested on Docker for Mac Version 1.12.3 (13776) running macOS Sierra 10.12.1 (16B2555)