Initializing #Docker Volumes in #WindowsContainer

In my previous post about volumes in Windows containers, I demonstrated how to use volumes to clean up installation files. But I did not answer the question how to use volumes similar to those in Docker for Linux where you can define a volume and add files to it in Dockerfile. Remember it is not possible to create a volume on a non-empty directory in Windows Containers.

Unfortunately, when building a Windows container data is not persisted if the volume was defined before data was added to it. This can only be solved by storing the data somewhere else in the container and copying the files when the container starts. This is demonstrated by the following Dockerfile:

FROM microsoft/windowsservercore

SHELL ["powershell", "-Command"]

ADD bootstrap c:\bootstrap
ADD Invoke-Bootstrap.ps1 c:/
VOLUME c:\\data
ENTRYPOINT c:\Invoke-Bootstrap.ps1

As soon as the container starts, the PowerShell script Invoke-Bootstrap.ps1 copies all files from c:\bootstrap to the volume mounted in c:\data:

Copy-Item -Path c:\bootstrap\* -Destination c:\data -Recurse -Verbose

This code is published on GitHub.

Feedback is always welcome! If you'd like to get in touch with me concerning the contents of this article, please use Twitter.