Docker on Windows — Mounting Host Directories
The native release of Docker on Windows has been working well for me. One of the key use cases for me is to use Docker for development/debug/test cycles. To enable this and to work rapidly, it is important that you are able to map a directory from your local system, read that as Windows host machine, to your docker container. This is done via volume mounting and this post is a step by step guide to validate that it works.
This post assumes that you have access to the beta for Docker for Windows native application and have been able to get it up and running on your Windows 10 machine.
To prepare ourselves to test the volume mapping , we will need to have a directory available on our windows machine and some files to validate the whole process. This is what I want to do on my machine and the steps to do the initial groundwork are given below:
- I want to expose a folder in my D drive named data i.e. d:\data.
- I have created a few files in d:\data for e.g. file1.txt and file2.txt.
Even if you do not want to create the above folder and files, that is fine. I will assume that you know which folder you want to expose and make available to the container.
Here we go with the steps:
- In the System Tray, you should have the cute Docker whale swimming. Right click and select Settings.
2. In the Settings dialog that comes up, click on Shared Drives. This should be able to list down the drives that you have available on your Windows machine. In my case, I have C and D drives and I have chosen to share D:\ drive since I want to expose the D:\data folder to my containers.
3. Click on Apply. This will bring up the Credentials dialog and you will need to provide your current Windows credentials. Ensure that you give it correctly. I also suspect that you might need to be an Administrator.
4. I am usually not comfortable if I just click something called Apply and assume that it got done. Do we have a log file that has traced what has happened and which we can actually check? Yes, there is. From the Settings dialog, go to Diagnose and Feedback and click on the trace log. You should see some commands being executed to enable the drive sharing. I am attaching the log that I have on my machine:
5. Now, that it looks good. We can go to a Powershell prompt or good old command line to run a few Docker containers to validate our volume mapping. I am assuming here that you already have pulled a Docker image for use. In my case, I have the Alpine Linux image available locally, so I am just going to run that.
6. To mount our host directory (d:\data) in a container , we are going to use the -v (volume) flag while running the container. A sample run is shown here.
D:\>docker run -v d:/data:/data alpine ls /data
file1.txt
file2.txt
If you find the value that we passed to -v flag confusing, it reads like this:
-v <host-directory>:<container-path>
7. Let us go into the container now:
docker run -it -v d:/data:/data alpine /bin/sh/ # ls
bin dev home linuxrc mnt root sbin sys usr
data etc lib media proc run srv tmp var/ # ls data
file1.txt file2.txt/ # cat data/file1.txt
this is file1 data
Everything looks good. You could even look at creating files from the container and you will find that it reflects back into the windows folder. For e.g. from the container shell, I did the following:
/# cat > file3.txt
this is file3 data
Then when I look at my folders in d:\data, I see the new file that I created from inside the folder.
Hope this note was useful. Check out my Docker tutorial for more stuff around that.