Search code examples
dockerdocker-for-windows

Docker for Windows: How to initialize a volume with the contents of a directory of an image?


My image has the following directory: C:\MyDir. This directory has some pre-defined contents which were created while building the image.

I wanted to have changes to this directory persisted, so I tried to use volumes but, when I do and upon the container's creation, C:\MyDir is empty. As I understand from the Docker documentation it seems that if I use an anonymous volume, the actual contents (from the image) of C:\MyDir will be reflected in the volume during its creation and therefore inside the container they should be present, however they aren't - no files seemed to have been copied from the image. Of course, after writing some new files there already inside the container, the files are reflected in the volume.

Any idea on how can I achieve this? - to have my running changes persisted while still keeping the original files from the image?


Solution

  • At the moment, Docker for Windows does not support this behavior. This is currently being tracked by the issue Windows: cannot put data in volumes with docker build on GitHub.

    Currently, the closest alternative is to do something like this:

    #escape=`
    FROM microsoft/windowsservercore
    SHELL [ "powershell", "-Command" ]
    COPY ".\starter-config" "C:\starter-config"
    CMD if ((Get-ChildItem 'C:\config').Length -eq 0) {`
            Copy-Item 'C:\starter-config\*' 'C:\config\';`
        };`
        & 'C:\RunTheServer.ps1'