In this blog post, I’ll show you how to copy files between a running Windows Container and the Container Host with 3 methods that available for us.
Windows Containers and docker offers 3 methods to copy files between Containers and the outside world which I listed below:
- Docker CP – Using the docker cp command we can copy files from a folder on the Container Host and a running Container
- Mount Volume – This Is a permanent solution In case you need to Container to access data all the time from a shared storage on the Container Host (database)
- Download Files from the Internet – You could also download files directly from the Internet to your Container using a PowerShell command
Option 1 – Direct Copy Files
In the example below, I’m using the docker cp command and copy a zip file from my Container host to a running Container called web01
docker cp C:\online.zip web01:/
As you can see the once done the file appears on the Container’s C:\ Drive
Once the file appears on the Container I can extract it
Option 2 – Mount Volume
By using Data Volumes, the data Is visible both on the Container Host and on the Container at the same time, It also allows us to transfer data between the Host And Container.
The first thing you need to know Is that all data Volumes are stored In the path below on the Container Host.
Next, we need to create a data volume using the docker command below:
Docker volume create --name volume01
To view all volumes type:
Docker volume ls
To attach a Volume to a Container use:
docker run --name test01 -it -v c:\programdata\docker\volumes\volume01:c:\volume01 microsoft/windowsservercore powershell.exe
And now and I can see my volume under my C:\ drive Inside the Container
Option 3 – Download Files from the Internet
If you simply need to download a file from the Internet and you don’t want to copy it first to your Container Host you can use the wget cmdlet and download it to the Container directly.
Below, I’m using wget to download a zip file from a website
wget -Uri https://download.sysinternals.com/files/SysinternalsSuite.zip -UseBasicParsing
To summarize this post, you could see that there are few ways to copy files and each one will fit a different scenario.