Instructor Demo: Basic Volume Usage
In this demo, we'll illustrate:
- Creating, updating, destroying, and mounting docker named volumes
- How volumes interact with a container's layered filesystem
- Usecases for mounting host directories into a container
Using Named Volumes
Create a volume, and inspect its metadata:
PS: node-0 Administrator> docker volume create demovol PS: node-0 Administrator> docker volume inspect demovol [ { "Driver": "local", "Labels": {}, "Mountpoint": "C:\\ProgramData\\docker\\volumes\\demovol\\_data", "Name": "demovol", "Options": {}, "Scope": "local" } ]We can see that by default, named volumes are created under
C:\\ProgramData\\docker\\volumes\\<volume name>\\_data.Run a container that mounts this volume, and list the filesystem therein:
PS: node-0 Administrator> docker container run ` -it -v demovol:C:\demo microsoft/nanoserver powershell PS C:\> ls Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d----l 8/26/2018 12:10 AM demo d----- 8/26/2018 12:11 AM Program Files d----- 7/16/2016 12:09 PM Program Files (x86) d-r--- 8/26/2018 12:11 AM Users d----- 8/26/2018 12:11 AM Windows -a---- 11/20/2016 11:32 AM 1894 License.txtThe
demodirectory is created as the mountpoint for our volume, as specified in the flag-v demovol:C:\demo.Put a file in this volume:
PS C:\> echo 'dummy file' > C:\demo\mydata.datExit the container, and list the contents of your volume on the host:
PS: node-0 Administrator> ls C:\\ProgramData\\docker\\volumes\\demovol\\_dataYou'll see your
mydata.datfile present at this point in the host's filesystem. Delete the container:PS: node-0 Administrator> docker container rm -f <container ID>The volume and its contents will still be present on the host.
Start a new container mounting the same volume, and show that the old data is present in your new container:
PS: node-0 Administrator> docker container run ` -it -v demovol:C:\demo microsoft/nanoserver powershell PS C:\> cat C:\demo\mydata.datExit this container, and inspect its mount metadata:
PS: node-0 Administrator> docker container inspect <container ID> "Mounts": [ { "Type": "volume", "Name": "demovol", "Source": "C:\\ProgramData\\docker\\volumes\\demovol\\_data", "Destination": "c:\\demo", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ],Here we can see the volumes and host mountpoints for everything mounted into this container.
Clean up by removing that volume:
PS: node-0 Administrator> docker volume rm demovolYou will get an error saying the volume is in use - docker will not delete a volume mounted to any container (even a stopped container) in this way. Remove the offending container first, then remove the volume again.
Mounting Host Paths
In a fresh directory
myweb, make a Dockerfile to make a simple containerization of nginx:FROM microsoft/windowsservercore RUN ["powershell", "wget", "http://nginx.org/download/nginx-1.11.6.zip", \ "-UseBasicParsing", "-OutFile", "c:\\nginx.zip"] RUN ["powershell", "Expand-Archive", "c:\\nginx.zip", "-Dest", "c:\\nginx"] WORKDIR c:\\nginx\\nginx-1.11.6 ENTRYPOINT ["powershell", ".\\nginx.exe"]Build this image, and use it to start a container that serves the default nginx landing page:
PS: node-0 myweb> docker image build -t nginx . PS: node-0 myweb> docker container run -d -p 5000:80 nginxVisit the landing page at
<node-0 public IP>:5000to confirm everything is working, then remove this container.Create some custom HTML for your new website:
PS: node-0 myweb> echo "<h1>Hello Wrld</h1>" > index.htmlThe HTML served by nginx is found in the container's filesystem at
C:\nginx\nginx-1.11.6\html. Attempt to mount yourmywebdirectory at this path:PS: node-0 myweb> docker container run -d -p 5000:80 ` -v C:\Users\Administrator\myweb:C:\nginx\nginx-1.11.6\html ` nginxThe container startup fails, since the
C:\nginx\nginx-1.11.6\htmldirectory isn't empty.Modify your Dockerfile to remove the default content in this directory by adding one more line at the end:
RUN ["powershell", "Remove-Item", "C:\\nginx\\nginx-1.11.6\\html\\*"]Rebuild your nginx image.
Launch an nginx container again based on this new nginx image, and mount in your custom html:
PS: node-0 myweb> docker container run -d -p 5000:80 ` -v C:\Users\Administrator\myweb:C:\nginx\nginx-1.11.6\html ` nginxVisit your webpage
<node 0 public IP>:5000; you should be able to see your custom webpage.There's a typo in your custom html. Fix the spelling of 'world' in your HTML, and refresh the webpage; the content served by nginx gets updated without having to restart or replace the nginx container.
Conclusion
In this demo, we saw two key points about volumes: first, they persist and provision files beyond the lifecycle of any individual container. Second, we saw that manipulating files on the host that have been mounted into a container immediately propagates those changes to the running container; this is a popular technique for developers who containerize their running environment, and mount in their in-development code so they can edit their code using the tools on their host machine that they are familiar with, and have those changes immediately available inside a running container without having to restart or rebuild anything.