Working volumes

Using mounted volumes (folders/files) in docker makes it a lot easier to pass data between the host and the container environment.  Any changes in the host environment to a folder and all its contents, or a file are immediately reflected in the container environment.  Conversely any changes in the container environment to a folder and all its contents, or a file are immediately reflected in the host environment.  To test this will run two instances of tomcat, one mounting a file and other mounting a directory that a file exists within, the commands are (we are assuming that you have previously executed the following commands - docker pull tomcat, followed by docker tag tomcat:latest tomcat-server)

Mount a file
docker run -p 8083:8080 -dit --name ti_1 -v //c/Users/Selvyn/demo/vm_share/html1/index.html:/usr/local/tomcat/webapps/html/index.html tomcat-server
Mount a folder
docker run -p 8083:8080 -dit --name ti_2 -v //c/Users/Selvyn/demo/vm_share/html1:/usr/local/tomcat/webapps/html tomcat-server

The docker run -v will mount a volume, but on Windows it is defaulted to C:\Users\<user home>.  On windows when you type docker run -v <host path>:<container path> the container gets a new mounted location <container path> that is mapped to <host path>.  If you specify the same <host path> in another docker run for the same or different image, the new container will share the same directories and files as all other containers that reference <host path>.  In actual fact on Windows <host path> is a volume within the docker-machine not within the Windows host itself; this is an anomaly because in Linux it would be a path within the Linux host environment. 

You can actually use docker volume create to create a new volume, then either use docker run -v <new volume> which will mount it to /<new volume> within the container or, docker run -v <new volume>:<container path> which will mount it to <container path> within the container.

Notice the "//c/" in the host path.  This is mandatory on Windows and indicates the drive letter.


Testing the shared folder/file

If you edit the file within the host environment, when you refresh your browser you should see the updates in the two containers.

If you edit the file from within one of the containers, you should see that that file has been changed in the host and in the other container (test by refreshing the browser).