- 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.
- Qt 5 and OpenCV 4 Computer Vision Projects
- Docker and Kubernetes for Java Developers
- LabVIEW 2018 虛擬儀器程序設計
- Java完全自學教程
- 秒懂設計模式
- 用Flutter極速構建原生應用
- Mastering Drupal 8 Views
- 自然語言處理Python進階
- Visual C#通用范例開發金典
- Mastering JavaScript High Performance
- 算法圖解
- After Effects CC技術大全
- Microsoft HoloLens By Example
- Java自然語言處理(原書第2版)
- Laravel Design Patterns and Best Practices