官术网_书友最值得收藏!

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:

  1. Execute the following command:
docker volume create <volumeName>
  1. 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:

  1. 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
Omitting the source=<volumeName> parameter will cause the creation of an anonymous volume that can be accessed using the volume ID later. Bear in mind that if you run a container with the --rm option, anonymous volumes will be automatically deleted when the container is exited.
  1. 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
  1. 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!
  1. 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
  1. 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!
It is also possible to use the VOLUME command in a Dockerfile in order to force the automatic creation of a volume on container start, even if the --mount parameter was not provided for the docker run command. This is useful if you would like to explicitly inform others of where state data is stored by your application and also when you need to ensure that the layer filesystem is not introducing additional overhead. You can find an example of the VOLUME command's usage in the following Dockerfile in the repository for this book: https://github.com/PacktPublishing/Hands-On-Kubernetes-on-Windows/blob/master/Chapter02/03_MongoDB_1903/Dockerfile#L44.

In the next subsection, we'll take a quick look at how to remove volumes using the Docker CLI.

主站蜘蛛池模板: 杨浦区| 湾仔区| 石柱| 常德市| 大名县| 平塘县| 玉环县| 林州市| 旅游| 浮梁县| 广东省| 行唐县| 邳州市| 贵港市| 日喀则市| 阿克苏市| 鄂伦春自治旗| 剑川县| 宣威市| 壤塘县| 叙永县| 天峨县| 隆安县| 孟津县| 石台县| 开平市| 威海市| 锡林浩特市| 河北省| 瓮安县| 西乌珠穆沁旗| 兰西县| 嘉黎县| 浮梁县| 阿克苏市| 叶城县| 西平县| 蓬莱市| 康乐县| 竹山县| 石嘴山市|