moby: Windows: cannot put data in volumes with `docker build`

What works on Linux

cat .\Dockerfile.linux
FROM ubuntu

RUN mkdir /test && touch /test/file.txt

VOLUME /test

docker build -t vol-test -f .\Dockerfile.linux .
docker run vol-test
docker volume ls
...
docker volume ls
DRIVER              VOLUME NAME
local               7715b61dd901682ed48825ae2db8a24d31bdf3ea0bd0a5098a4c8de2e624634f
docker run -ti -v 7715b61dd901682ed48825ae2db8a24d31bdf3ea0bd0a5098a4c8de2e624634f:/foo ubuntu ls /foo
file.txt

What works on Windows

docker run -v test-vol:C:\test microsoft/nanoserver powershell -c New-Item C:\test\foo.txt
    Directory: C:\test
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/22/2017  10:54 PM              0 foo.txt

docker run -v test-vol:C:\foo microsoft/nanoserver powershell -c ls c:\foo
    Directory: C:\foo
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/22/2017  10:54 PM              0 foo.txt

What doesn’t work on Windows

cat .\Dockerfile.win
FROM microsoft/nanoserver

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

RUN mkdir C:\test ; New-Item -Type File C:\test\foo.txt

VOLUME C:\\test

docker build -t win-vol -f .\Dockerfile.win .
...
docker run win-vol
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: container 756d36348d89bd4bae0ee91e44a91f6ee2ce794d588efd5705db68ec5fef241e encountered an error during Start: failure in a Windows system call: The connection with the Virtual Machine hosting the container was closed. (0xc037010a).

Describe the results you received:

Error

Describe the results you expected:

That docker run of image with VOLUME declaration works, and will product volume with data in it.

Output of docker version:

Docker 1.13.0 for both Linux and Windows

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 3
  • Comments: 18 (6 by maintainers)

Most upvoted comments

@alparamonov that approach is right, this example works as expected:

# escape=`
FROM microsoft/windowsservercore
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';"]

VOLUME C:/agent
RUN 'test' | Out-File c:\\test.txt
CMD cp c:\\test.txt c:\\agent; `
    ls c:\\agent

Build, and when you run you see the file in the volume:

PS C:\temp\vol> docker run --rm temp

    Directory: C:\agent

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/24/2017  11:41 AM             14 test.txt

@thaJeztah thanks for clarifying! I can actually live with the fact that the destination directory of a mount must be empty.

But is there also an explanation what happens when @alparamonov moved the VOLUME command to the top of the Dockerfile? Because I can reproduce his findings.

@sixeyed That seems weird because a similar Dockerfile produces the expected result under Linux. I would expect both platforms to behave identically.

[centos@docker01 test]$ cat Dockerfile
FROM ubuntu:xenial

RUN mkdir /agent \
 && touch /agent/foobar

VOLUME /agent
[centos@docker01 test]$ docker build -t test .
Sending build context to Docker daemon 2.048 kB
Step 1/3 : FROM ubuntu:xenial
 ---> 4ca3a192ff2a
Step 2/3 : RUN mkdir /agent  && touch /agent/foobar
 ---> Using cache
 ---> 5af4a3e7d4be
Step 3/3 : VOLUME /agent
 ---> Using cache
 ---> b447579f2546
Successfully built b447579f2546
[centos@docker01 test]$ docker run --rm test bash -c "ls -l /agent/"
total 0
-rw-r--r--. 1 root root 0 Jan 23 11:03 foobar
[centos@docker01 test]$