- Hands-On Kubernetes on Windows
- Piotr Tylenda
- 513字
- 2021-06-24 16:54:01
Creating and mounting volumes
Creating a new volume can be performed explicitly using the docker volume create command. It is also possible to create named and anonymous volumes automatically when the container starts. To manually create a Docker named volume, follow these steps:
- Execute the following command:
docker volume create <volumeName>
- After creation, you can inspect the details of the volume using the docker volume inspect command:
As you can see, the volume data is stored as a regular directory in the host filesystem when using the default local driver.
To mount a volume to a container, you have to use the --mount or --volume (short parameter: -v) parameters for the docker run command. Originally, --volume was used for stand-alone containers, whereas --mount was used for swarm containers, but starting with Docker 17.06, --mount can also be used for standalone containers and is the recommended practice as it provides more robust options. More about these flags can be found in the official documentation: https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag.
Follow these steps to learn how to mount a volume:
- Assuming that you would like to mount test-named-volume from the previous example in a new PowerShell container under the C:\Data directory, you have to specify the --mount parameter, as follows:
docker run -it --rm `
--isolation=process `
--mount source=test-named-volume,target=C:\Data `
mcr.microsoft.com/powershell:windowsservercore-1903
- After the container has started and the terminal has been attached, try creating a simple file in the directory where a volume has been mounted:
echo "Hello, Volume!" > C:\Data\test.txt
- Now, exit the container (which will cause it to stop and be automatically removed due to the --rm flag) and inspect the volume directory on the host:
PS C:\WINDOWS\system32> cat C:\ProgramData\Docker\volumes\test-named-volume\_data\test.txt
Hello, Volume!
- To demonstrate that the named volume can be easily mounted in another container, let's create a new container based on the mcr.microsoft.com/windows/servercore:1903 image and with a volume mount target that's different from the one in the previous example:
docker run -it --rm `
--isolation=process `
--mount source=test-named-volume,target=C:\ServerData `
mcr.microsoft.com/windows/servercore:1903
- If you inspect the volume directory in the container, you will notice that the test.txt file is present and contains the expected content:
C:\>more C:\ServerData\test.txt
Hello, Volume!
In the next subsection, we'll take a quick look at how to remove volumes using the Docker CLI.
- Effective C#:改善C#代碼的50個有效方法(原書第3版)
- Unity 2020 Mobile Game Development
- JavaScript前端開發與實例教程(微課視頻版)
- YARN Essentials
- 精通Python設計模式(第2版)
- The DevOps 2.5 Toolkit
- Python Web數據分析可視化:基于Django框架的開發實戰
- Learning jQuery(Fourth Edition)
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- 移動互聯網軟件開發實驗指導
- 智能手機故障檢測與維修從入門到精通
- RocketMQ實戰與原理解析
- Python 3快速入門與實戰
- Python Social Media Analytics
- DevOps 精要:業務視角