Windows 2022
Sponsored Link

Docker : Use External Storage2022/01/21

 
When Container is removed, data in it are also lost, so it's necessary to use external filesystem in Containers as persistent storage if you'd like to save data.
[1] It's possible to mount a directory on Docker Host into Containers.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

# create a directory for data
PS C:\Users\Administrator> mkdir C:\docker-storage 

# create a test file
PS C:\Users\Administrator> Write-Output 'Docker Persistent Storage Test' | Out-File -Encoding default C:\docker-storage\index.html 

# run a container with mounting the directory above on [C:\disk01]
PS C:\Users\Administrator> docker run -it -v C:\docker-storage:C:\disk01 mcr.microsoft.com/windows/servercore:ltsc2022 
Microsoft Windows [Version 10.0.20348.473]
(c) Microsoft Corporation. All rights reserved.

C:\> dir C:\ 
 Volume in drive C has no label.
 Volume Serial Number is 9864-D27B

 Directory of C:\

01/20/2022  07:36 PM    <DIR>          disk01
05/08/2021  02:26 AM             5,510 License.txt
01/16/2022  05:18 AM    <DIR>          Program Files
01/15/2022  09:05 PM    <DIR>          Program Files (x86)
01/15/2022  09:23 PM    <DIR>          Users
01/20/2022  07:38 PM    <DIR>          Windows
               1 File(s)          5,510 bytes
               5 Dir(s)  21,291,532,288 bytes free

C:\> type C:\disk01\index.html 
Docker Persistent Storage Test
[2] It's also possible to configure external storage by Docker Data Volume command.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

# create [volume01] volume
PS C:\Users\Administrator> docker volume create volume01 
volume01

# display volume list
PS C:\Users\Administrator> docker volume ls 
DRIVER    VOLUME NAME
local     volume01

# display details of [volume01]
PS C:\Users\Administrator> docker volume inspect volume01 
[
    {
        "CreatedAt": "2022-01-20T19:42:23-08:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "C:\\ProgramData\\docker\\volumes\\volume01\\_data",
        "Name": "volume01",
        "Options": {},
        "Scope": "local"
    }
]

# run a container with mounting [volume01] to [C:\disk01] on container
PS C:\Users\Administrator> docker run -it -v volume01:C:\disk01 mcr.microsoft.com/windows/servercore:ltsc2022 powershell 
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\> ls C:\ 

    Directory: C:\


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         1/20/2022   7:42 PM                disk01
d-r---         1/16/2022   5:18 AM                Program Files
d-----         1/15/2022   9:05 PM                Program Files (x86)
d-r---         1/15/2022   9:23 PM                Users
d-----         1/20/2022   7:45 PM                Windows
-a----          5/8/2021   3:26 AM           5510 License.txt


PS C:\> echo "Docker Volume test" | Out-File C:\disk01\testfile.txt -Encoding Default 
PS C:\> exit 

PS C:\Users\Administrator> Get-Content C:\ProgramData\docker\volumes\volume01\_data\testfile.txt 
Docker Volume test


# possible to mount from other containers
PS C:\Users\Administrator> docker run -v volume01:C:\volume01 mcr.microsoft.com/windows/servercore:ltsc2022 powershell -c "Get-Content C:\volume01\testfile.txt" 
Docker Volume test

# to remove volumes, do like follows
PS C:\Users\Administrator> docker volume rm volume01 
Error response from daemon: remove volume01: volume is in use - [2641c52ffcf4859399840597f0b0aa3ff04155c950ff79c2cb5c9fa32c962ace, e0c015ca1a540ab48aeb835346966cf3578629f680d286a91f5262f844f24a12]

# if some containers are using the volume you'd like to remove like above,
# it needs to remove target containers before removing a volume
PS C:\Users\Administrator> docker rm 2641c52ffcf4859399840597f0b0aa3ff04155c950ff79c2cb5c9fa32c962ace 
PS C:\Users\Administrator> docker rm e0c015ca1a540ab48aeb835346966cf3578629f680d286a91f5262f844f24a12 

PS C:\Users\Administrator> docker volume rm volume01 
volume01
Matched Content