Windows 2025
Sponsored Link

Docker : Use External Storage2024/12/16

 

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:ltsc2025 
Microsoft Windows [Version 10.0.26100.2605]
(c) Microsoft Corporation. All rights reserved.

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

 Directory of C:\

12/15/2024  06:07 PM    <DIR>          disk01
12/08/2024  02:26 PM             5,647 License.txt
12/08/2024  02:48 PM    <DIR>          Program Files
12/08/2024  02:26 PM    <DIR>          Program Files (x86)
12/08/2024  02:48 PM    <DIR>          Users
12/15/2024  06:07 PM    <DIR>          Windows
               1 File(s)          5,647 bytes
               5 Dir(s)  136,180,486,144 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": "2024-12-15T18:09:47-08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "C:\\ProgramData\\docker\\volumes\\volume01\\_data",
        "Name": "volume01",
        "Options": null,
        "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:ltsc2025 powershell 
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\> ls C:\ 

    Directory: C:\


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        12/15/2024   6:09 PM                disk01
d-r---         12/8/2024   2:48 PM                Program Files
d-r---         12/8/2024   2:26 PM                Program Files (x86)
d-r---         12/8/2024   2:48 PM                Users
d-----        12/15/2024   6:11 PM                Windows
-a----         12/8/2024   2:26 PM           5647 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:ltsc2025 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 - [c25c556de0fc5e3b2caa739d56938da37a2edac8599e421e2b0790d420e28e8e, 9f979d883675f5821df9204420a233ceb685276cfb4c1bea6cb7b0320bad42bb]

# 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 c25c556de0fc5e3b2caa739d56938da37a2edac8599e421e2b0790d420e28e8e 
PS C:\Users\Administrator> docker rm 9f979d883675f5821df9204420a233ceb685276cfb4c1bea6cb7b0320bad42bb 

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