Windows 2019
Sponsored Link

Docker : Use Persistent Storage
2019/02/19
 
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.
# 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:1809
Microsoft Windows [Version 10.0.17763.1637]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\> dir C:\ 
 Volume in drive C has no label.
 Volume Serial Number is 6CA1-73D3

 Directory of C:\

12/15/2020  08:07 PM    <DIR>          disk01
05/06/2020  08:48 PM             5,510 License.txt
12/03/2020  06:14 PM    <DIR>          Program Files
12/03/2020  06:12 PM    <DIR>          Program Files (x86)
12/03/2020  06:15 PM    <DIR>          Users
12/15/2020  08:09 PM    <DIR>          Windows
               1 File(s)          5,510 bytes
               5 Dir(s)  21,199,335,424 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.
# 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": "2020-12-15T20:17:47-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:1809 powershell

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\> ls C:\ 

    Directory: C:\

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       12/15/2020   8:17 PM                disk01
d-r---        12/3/2020   6:14 PM                Program Files
d-----        12/3/2020   6:12 PM                Program Files (x86)
d-r---        12/3/2020   6:15 PM                Users
d-----       12/15/2020   8:31 PM                Windows
-a----         5/6/2020   9:48 PM           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:1809 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 - [1864ecc444276667189e5c5cd099e38554a316d73b755670a5835742df673588, 89c96e683cb5ebf4a0bdb8614a0f53bf81a7dd0c345a2e45ce0be7cd61e0434c]

# 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 1864ecc444276667189e5c5cd099e38554a316d73b755670a5835742df673588

PS C:\Users\Administrator>
docker rm 89c96e683cb5ebf4a0bdb8614a0f53bf81a7dd0c345a2e45ce0be7cd61e0434c
PS C:\Users\Administrator>
docker volume rm volume01

volume01
Matched Content