Using Docker Volumes from Windows Hosts

Docker is one of those tools that have become indispensable for me as a developer. I use it on a regular-basis in my day-job – spinning up databases for local development, images to run database migrations, for integration tests and so much more. Today, let’s look at how you can setup volumes in docker so that you can take care of filesystem between your Windows host and UNIX/Linux-based containers.

What are Volumes?

Docker containers, by nature, are ephemeral. You spin up a docker container to do a task and after it is done, you want it to go away, not taking up precious resources on your computer. Contrast this idea with that of virtual machines. VMs are usually meant to be longer-lasting. You still name them, maintain, care for, and feed them, much like you would your physical machines. So, with the ephemeral nature of docker containers, you need some sort of a provision to save your data – save your work – long after the container that did that work is gone. Enter docker volumes. Volumes allow you to

  • Save data from a container onto your host machine.
  • Transfer data out of your host machine and into a container to be worked within that container.
  • Share data between your host computer and multiple docker containers.

Setup a Docker Volume

From the command-line, you use the -v switch to create volumes. The -v switch has three parts, two that are required and one that’s optional: [the-directory-on-your-host]:[how-you-want-this-exposed-in-your-containers-filesystem]:[comma-separated-list-of-options]. You can read the manual for all the available options.

For this example, we’re using the Ubuntu official image. Run the command docker run -it ubuntu

Windows command line showing a docker run command on the official ubuntu image and a directory listing inside that container.

To avoid the ubuntu container from immediately exiting after creation, we’re using the -it switches to tell docker to start the container in interactive mode and allow user-input in its bash shell. In my screenshot above, I’ve ran the ls command to list all the files and folders at the root of the ubuntu filesystem.

Now, let’s try that again, now with the -v switch. I have a test-share folder on my host machine that I want to access from the ubuntu container. For that, I enter the command: docker run -it -v /c/test-share:/share-from-windows ubuntu. Here, the /c/test-share is a reference to my “test-share” folder on my C: drive. Next, /share-from-windows is how ubuntu will mount this directory in the container. When you run this, it should yield output like what I’ve shown below:

This time around, when I run the ls command in the container, it shows a new directory named share-from-windows. I change directory (cd) into it and run another ls and I see the hello.txt file that I have within that folder.

docker container bash output juxtaposed with the same folder and file  directly open in windows

Closing Thoughts

It is not very obvious how to specify your host Windows directory in this command and the official documentation doesn’t provide any such detail. For example, if you wanted to map the location D:\my\test\folder, you’ll need to refer to it as /d/my/test/folder within the docker command. If you specify it the “windows way,” it will yield an error stating that you have invalid characters in your volume specification.

In future episodes, we can dig a bit deeper and look at some inter-container communications, both on the network and filesystem levels.

Leave a Comment

Your email address will not be published. Required fields are marked *