How to Copy Data from Host to Named Volume on Docker for Windows with Windows Containers

I am trying to copy existing data from my host machine into a Windows Volume.

I have found instructions that work for linux, but not on windows. Here are the linux instructions, as found here:

docker container create --name dummy -v myvolume:/root hello-world
docker cp c:\myfolder\myfile.txt dummy:/root/myfile.txt
docker rm dummy

On Windows, I have to change the volume declaration to:
docker container create --name dummy -v myvolume:C:/root hello-world
Otherwise, I get this error message:
Error response from daemon: invalid volume specification: ‘myvolume:/root’

Once the container has been properly created, the copy command fails:
docker cp C:\Users\local_user\Documents\Docker\hello-world.txt dummy:C:\root\myfile.txt
Error Message:
Error: No such container:path: dummy:C:\root

Any assistance would be greatly appreciated.

Have you checked the path inside the container before running the copy command?

I don’t use windows containers usually don’t remember the proper syntax to refer to a container path but I cans ee that you sometimes used forward slashes, sometimes backslashes.

Try this

docker container create --name dummy -v myvolume:C:/root hello-world

The source: Docker / Windows Container: how to mount a host folder as data volume on Windows 2016 - Super User

but I am sure it is also somewhere in Microsoft’s documentation I just tried a very quick search.

Ps.: Please, use code blocks like I did to make sure all your commands are shown as you post them

Hello, thank you for your response.

As I said, I don’t have any issues creating the volume, but populating it with data.

Here is the sequence using all forward slashes. In the below example, the container and volume are created successfully, yet I cannot add data to the volume.

PS C:\Users\local_user> docker container create --name dummy -v myvolume:C:/root mcr.microsoft.com/windows/servercore:1809
128a522a4d5af5b05998e84a4932028d5063b11021490bd65a1265a121ec295f
PS C:\Users\local_user> docker cp C:/Users/local_user/Documents/Docker/hello-world.txt dummy:C:/root/hello-world.txt
Error: No such container:path: dummy:C:\root

I switched to windows/servercore so that I could run an interactive terminal inside it. When I do, no data is present in the containers “C:/root” directory:

PS C:\Users\local_user>  docker run -it -v myvolume:C:/root mcr.microsoft.com/windows/servercore:1809 powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\> ls


    Directory: C:\


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-r---         2/7/2023   2:46 AM                Program Files
d-----         2/7/2023   2:44 AM                Program Files (x86)
d-----        3/20/2023   3:26 PM                root
d-r---         2/7/2023   2:49 AM                Users
d-----        3/20/2023   3:27 PM                Windows
-a----         1/6/2023   9:20 PM           5647 License.txt


PS C:\> cd .\root\
PS C:\root> ls

Since in your original post you could not share how you checked the folder inside the container, I had to rule out the possibility of creating the volume at a wrong path either because of a bug or incorrect syntax. The fact that a command doesn’t throw an error doesn’t mean it worked correctly.

Now that you shared the path existed in the container, we can focus on the cp command. I can’t try it now, since I am preparing to sleep and I will have to turn my Windows on, but I can try it tomorrow.

Until that I found this:

Wich seems the same issue and it is two years old.

You can also try something else, but it is a guess again. I don’t see that in your shared commands so if you haven’t tried that combination yet, you could try to create container again as you always did with the drive letter, but don’t use the drive letter in the cp command. As far as I know ther is only one drive in a Windows container, although there are some tricks to create another (we discussed it somewhere on the forum before), it worth a try to drop th drive letter and use the path only in the cp command.

One more thing I noticed, which shouldn’t be a problem on Linux is that you just create the container but don’t run it except when you checked the path inside the container. On Linux, I know you can copy to and from a stopped container, but I am not sure if that works with windows containers as well. So again, if you haven’t tried it yet, you could do that too. After creating the container, just run docker start dummy