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

  • Docker on Windows
  • Elton Stoneman
  • 324字
  • 2021-07-02 12:47:58

Mounting volumes from host directories

You use the --volume option to explicitly map a directory in a container from a known location on the host. The target location in the container can be a directory created with the VOLUME command, or any directory in the container's filesystem. If the target location already exists in the Docker image, it is hidden by the volume mount, so you won't see any of the image files.

I'll create a dummy configuration file for my app in a directory on the C drive on my Windows machine:

PS> mkdir C:\app-config | Out-Null
PS> echo 'VERSION=18.09' > C:\app-config\version.txt

Now I'll run a container which maps a volume from the host, and read the configuration file which is actually stored on the host:

> docker container run `
--volume C:\app-config:C:\app\config `
dockeronwindows/ch02-volumes:2e `
type C:\app\config\version.txt
VERSION=18.09

The --volume option specifies the mount in the format {source}:{target}. The source is the host location, which needs to exist. The target is the container location, which doesn't need to exist, but the existing contents will be hidden if it does exist.

Volume mounts are different in Windows and Linux containers. In Linux containers, Docker merges the contents from the source into the target, so if files exist in the image, you see them as well as the contents of the volume source. Docker on Linux also lets you mount a single file location, but on Windows you can only mount whole directories.

Volume mounts are useful for running stateful applications in containers, like databases. You can run SQL Server in a container, and have the database files stored in a location on the host, which could be a RAID array on the server. When you have schema updates, you remove the old container and start a new container from the updated Docker image. You use the same volume mount for the new container, so that the data is preserved from the old container.

主站蜘蛛池模板: 马公市| 五莲县| 南华县| 革吉县| 石河子市| 黑水县| 商河县| 日喀则市| 自治县| 扶沟县| 洪泽县| 中宁县| 通海县| 宝丰县| 澄迈县| 吉林省| 屏南县| 斗六市| 溧水县| 溆浦县| 黄平县| 河北省| 东辽县| 灯塔市| 永修县| 甘孜县| 台山市| 张掖市| 西乡县| 淮南市| 六安市| 临湘市| 遂昌县| 洛阳市| 芦溪县| 鹿泉市| 曲松县| 株洲市| 万载县| 泾川县| 蚌埠市|