Date

Copy Files | Folder Between Container Host and Container

Summary

In this article we will learn about different ways to copy/move files from Container to Host and vice-versa

Applies to Widows Server 2016/2019 on physical server or virtual server with Docker EE (Enterprise Edition)

Different ways to move/copy files from Container to Host and vice-versa

  1. Create a new Volume and mount to Container Directory

docker volume ls

  • Create a new Volume: iisvol

docker volume create iisvol

iisvol

docker volume ls

DRIVER              VOLUME NAME

local               iisvol

  • Verify the properties of new volume: iisvol

docker volume inspect iisvol 

[
                               {
                                           "CreatedAt": "2020-06-12T12:07:07Z",
                                             "Driver": "local",
                                            "Labels": {},
                                           "Mountpoint": "C:\\ProgramData\\docker\\volumes\\iisvol\\_data",
Name": "iisvol",
                                           "Options": {},
                                           "Scope": "local"
                              }
]
  • To validate New Volume: iisvol, browse to the path on Container Host: C:\ProgramData\docker\volumes\
  • Create a container and mount Volume: iisvol to container directory: c:\inetpub\wwwroot\site

docker run -it -d -v iisvol:c:\inetpub\wwwroot\site --name=voltest solatiare:iis 

  • Inspect Container: voltest and validate mount volumes: ‘Mounts’
"Mounts": [
            {
                "Type": "volume",
                "Name": "iisvol",
                "Source": "C:\\ProgramData\\docker\\volumes\\iisvol\\_data",
                "Destination": "c:\\inetpub\\wwwroot\\site",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

Open powershell in the Container

docker exec -it voltest powershell

  • Copy License.txt file from C:\ to C:\inetpub\wwwroot\site

PS C:\> ls

    Directory: C:\

Mode                LastWriteTime         Length Name

—-                ————-         —— —-

d—–        5/12/2020   8:28 PM                inetpub

d-r—         5/7/2020   5:10 AM                Program Files

d—–         5/7/2020   5:08 AM                Program Files (x86)

d-r—         5/7/2020   5:11 AM                Users

d—–        5/12/2020   8:29 PM                Windows

-a—-         5/7/2020   4:48 AM           5510 License.txt

-a—-        5/12/2020   8:29 PM         168344 ServiceMonitor.exe

cp License.txt C:\inetpub\wwwroot\site 

cd  C:\inetpub\wwwroot\site 

PS C:\inetpub\wwwroot\site> ls

    Directory: C:\inetpub\wwwroot\site

Mode                LastWriteTime         Length Name

—-                ————-         —— —-

-a—-         5/7/2020   4:48 AM           5510 License.txt

  • License.txt file copied to the C:\inetpub\wwwroot\site is available on Container Host in C:\ProgramData\docker\volumes\iisvol\_data
  1. Create a Container and mount a local folder into Docker Container (-v)

Eg:

 docker run -it -d -p 8000:80 --name=IIS -v C:\Users\JimmyCater\solitaire\app:c:\inetpub\wwwroot\site mcr.microsoft.com/windows/servercore/iis:latest 

Container Name: IIS

Local Folder: C:\Users\JimmyCater\solitaire\app

Container Folder: c:\inetpub\wwwroot\site

—-

  • Copy the File/Folders

Form Host to Container

docker cp C:\Users\JimmyCater\Documents\docker-windows-getting-started\materials\solitaire\app\HTML IIS:c:\inetpub\wwwroot\

Container Name: IIS

Local Folder: C:\Users\JimmyCater\Documents\docker-windows-getting-started\materials\solitaire\app\HTML

Container Folder: c:\inetpub\wwwroot\

docker exec -it IIS powershell

cd c:\inetpub\wwwroot\ 

dir 

    Directory: C:\inetpub\wwwroot

Mode                LastWriteTime         Length Name

—-                ————-         —— —-

d—–        5/24/2020   6:45 PM                HTML

d—–        5/24/2020   6:39 PM                site

-a—-        5/12/2020   8:28 PM            703 iisstart.htm

-a—-        5/12/2020   8:28 PM          99710 iisstart.png

-a—-        5/24/2020   6:00 PM             98 index.html

  • From Container to Host:

docker cp web1:C:\Windows\System32\winevt\Logs / 

Container Name: web1

Local Folder: C:\

Container Folder: C:\Windows\System32\winevt\Logs

3. To Specific Folder:

docker cp web1:C:\Windows\System32\winevt\Logs C:\ContainerEvtx 

Container Name: web1

Local Folder: C:\ContainerEvtx

Container Folder: C:\Windows\System32\winevt\Logs

Additional Information:

Once required files are moved and edited inside the Container, we can also Docker Images (with changes)

Creating a Docker images from running container

PS> docker commit <Container-Name> <Repository-Name:Tag>

Example

docker commit IIS ISSCostum:latest

AZmachina

Knowledge Shared = Knowledge2

We have created AZmachina blog to share our knowledge on Docker & Container and Kubernetes on Windows Servers with curious and enthusiastic novice learner. We hope that this will help them to take a swim in this vast ocean of Window Containers and Kubernetes

Happy Learning !
Recent Posts
Categories
Archives
Sumeet Kumar

Sumeet Kumar

I am Windows Core Engineer with 7+ years of experience in Windows Hyper-v, Failover Cluster, Windows Storage, Volume Shadow Copy (VSS), Docker & Containers on Windows Servers, Backup & Recovery, VMware vSphere EXSi & vCenter Server

RELATED

Articles

Leave a Reply

Your email address will not be published. Required fields are marked *