So as I once wrote in an earlier blog article, I am able to use Docker for Windows on my laptop at work, but I am not able to bind mount any windows directory into my Docker containers because of security settings preventing me to share the drive with the Hyper-V virtual machine.

The earlier blog article showed how I used the dperson/samba image to share a specific data volume. And unlike Joey:

who does not want to share food, I actually want to automatically share ALL of the containers via Samba instead of having to manually provide them :) The approach in my earlier article does work, but was a bit cumbersome if you

Recently I had some time for this and the result is an image for a container that you can easily use on any computer yourself.

Most importantly, I am now able to easily share this particular image with my colleagues allowing them to easily access the data in their data volumes also :) Best about this is that I can start working on creating more Docker converts within my department!! :)

The name of the image I created is gdiepen/volume-sharer and you can easily run it as follows:

docker run --name volume-sharer \  
           --rm \
           -d \
           -v /var/lib/docker/volumes:/docker_volumes \
           -p 139:139 \
           -p 445:445 \
           -v /var/run/docker.sock:/var/run/docker.sock \
           --net=host \
           gdiepen/volume-sharer

The different arguments provided to Docker are the following:

  • --name volume-sharer : Gives the container a recognizable name
  • --rm : Cleans up the container after it is stopped
  • -d : Makes the container run in the background
  • -v /var/lib/docker/volumes:/docker_volumes : Mounts the directory that holds the local Docker data volumes on the Docker host to the local folder /docker_volumes in the running container
  • -p 139:139 -p 445:445 : Publishes the ports used for Samba to the host
  • -v /var/run/docker.sock:/var/run/docker.sock : Mounts the Docker socket of the host system within the container to allow the container to access Docker information (i.e. volume creation/deletion)
  • --net=host : When running under windows, this argument is needed to publish the SMB ports to the Hyper-V virtual machine.
  • gdiepen/volume-sharer : The name of the of the image on DockerHub

You can also still provide additional arguments to the container (i.e. after gdiepen/volume-sharer in the command line). These are all the same as the ones you can provide to the dperson/samba image.

After the container is started, it will start listening for any docker events that are volume related (i.e. volumes created / removed / mounted). In the case of any such event, the Samba configuration file is rewritten and the running Samba daemon is instructed to reload the updated configuration.

How to use this in practice

Whenever you are on a system that does not allow for bind mounting of the host folders to your Docker container, you can start my volume-sharer container with the command line given above.

After (or before, does not really matter because the configuration is automagically updated :) ) you can start your own containers with either named or unnamed volume mounts.

By going to the SMB server localhost (\\localhost) in case you are not running on Windows (and do not need the --net=host argument), or the Hyper-V SMB server (\\10.0.75.2) in case you are running under Windows with --net=host argument you will be able to access and modify the contents of all of your named and unnamed data volumes.

You will see that any time you start a new container that has new (un)named volumes, automatically the list of shares will be updated in the SMB server, like magic:

A potential future feature I am thinking about is to create one separate share that contains all of the unnamed data volumes as you might have quite some of these if you have played around with Docker for some time ;)

Source-code

As usual, I have made the source-code available in a GitHub repository. You can use this to see how the inner details work. I have already linked this repository to my DockerHub account with an auto-build so that you can easily have the latest version of the image by just running a docker pull gdiepen/volume-sharer.

If you have any other (hopefully easier :) ) way of accessing the contents of the Docker data volumes, drop it in a comment as I would be happy to hear about it!