gjp311 - stock.adobe.com

Tip

How to copy files to Windows containers

Once you've deployed Windows containers in your production environment, you can start copying data and files to them using one of three methods, depending on file type and location.

There are three ways to copy files to Windows containers -- using Docker CP, mounting a volume or downloading files from the internet -- and each method has its own use cases.

You can use the Docker CP command-line tool from within the container to quickly copy a set of files from the container host. Application developers typically use the mounting volume method for production containers to ensure the applications hosted inside the container can host the data on a volume outside the container. Remember that containers are lightweight applications that you can recreate in a few seconds. You should never use containers to host data. Only use the Wget method if you must copy files to Windows containers from the internet without first copying them to the container host.

Use the Docker CP command line

The CP command line is part of the Docker Engine installation. If you must copy a zip file or a few other files, you can use the command below from within the container:

Docker cp C:\Temp\ThisFile.ZIP myprodcontainer:/

The cp command after Docker copies C:\Temp\ThisFile.ZIP from the container host machine to a running container called myprodcontainer on the C:\ drive. Once the zip file is available inside the container, you can extract it using the Expand-Archive command.

Mount a volume inside the container

Mounting a volume is effective because the mounted volume remains inside the container. To mount a container host volume, follow these steps.

Step 1: Create a data volume inside the container by running the command below:

Docker volume create --name volume03

This command creates a volume by the name Volume03. This volume has no data and isn't mounted to any of the host volumes yet.

To ensure the volume was created successfully, issue the Docker volume ls command inside the container. The Docker volume ls command lists the available volumes and their names.

Step 2: Attach the volume to the container host volume by executing the command below:

docker run --name thisvol -it -v c:\programdata\docker\volumes\volume03:c:\volume03 microsoft/windowsservercore powershell.exe

This command attaches C:\ProgramData\Docker\Volumes\Volume03 to C:\Volumes3 on the container. The volume will appear by the name of thisvol inside the container.

Containers host all data volumes and mounted volumes in the C:\ProgramData\Docker\Volumes folder.

Download files from the internet using Wget

The third way to copy files to Windows containers is to download files from a URL. If the files are hosted on a web portal, use the Wget command line to get them from the URL, as shown in the command below:

Wget –URI https://download.thisfile.com/files/File1.zip -UseBasicParsing

Copy files from Windows containers to the host

Generally speaking, you won't need to use the reverse copy method, as the data volumes are hosted outside the container, but you should know how, just in case. You can use the Docker CP command as shown below:

docker cp thiscontainer:/path/to/file /destionationhost/tofilelocation

It's important to understand that containers follow Linux-based architecture. When executing commands inside the container, you must use the complete command-set in lowercase. This isn't the case with Windows OS; you can use commands in both uppercase and lowercase. Similarly, when specifying the path inside the container, use / instead of \.

Next Steps

Use cURL and Wget to download network files from CLI

Dig Deeper on Containers and virtualization