Docker數(shù)據(jù)卷掛載命令volume(-v)與mount的使用總結(jié)
前言
用戶可以通過(guò)docker run的--volume/-v或--mount選項(xiàng)來(lái)創(chuàng)建帶有數(shù)據(jù)卷的容器,但這兩個(gè)選項(xiàng)有些微妙的差異,在這里總結(jié)梳理一下。
命令用法
--volume(-v)
參數(shù)--volume(或簡(jiǎn)寫為-v)只能創(chuàng)建bind mount。示例:
docker run --name $CONTAINER_NAME -it \ -v $PWD/$CONTAINER_NAME/app:/app:rw \ -v $PWD/$CONTAINER_NAME/data:/data:ro \ avocado-cloud:latest /bin/bash
注釋:
- 命令格式:[[HOST-DIR:]CONTAINER-DIR[:OPTIONS]]]
- 如果指定HOST-DIR則必須是絕對(duì)路徑,如果路徑不存在則會(huì)自動(dòng)創(chuàng)建
- 實(shí)例中的rw為讀寫,ro為只讀
--mount
參數(shù)--mount默認(rèn)情況下用來(lái)掛載volume,但也可以用來(lái)創(chuàng)建bind mount和tmpfs。如果不指定type選項(xiàng),則默認(rèn)為掛載volume,volume是一種更為靈活的數(shù)據(jù)管理方式,volume可以通過(guò)docker volume命令集被管理。示例:
docker run --name $CONTAINER_NAME -it \ --mount type=bind,source=$PWD/$CONTAINER_NAME/app,destination=/app \ --mount source=${CONTAINER_NAME}-data,destination=/data,readonly \ avocado-cloud:latest /bin/bash
注釋:
- 掛載volume命令格式:[type=volume,]source=my-volume,destination=/path/in/container[,...]
- 創(chuàng)建bind mount命令格式:type=bind,source=/path/on/host,destination=/path/in/container[,...]
- 如果創(chuàng)建bind mount并指定source則必須是絕對(duì)路徑,且路徑必須已經(jīng)存在
- 示例中readonly表示只讀
差異總結(jié)
創(chuàng)建bind mount和掛載volume的比較
對(duì)比項(xiàng) | bind mount | volume |
---|---|---|
Source位置 | 用戶指定 | /var/lib/docker/volumes/ |
Source為空 | 覆蓋dest為空 | 保留dest內(nèi)容 |
Source非空 | 覆蓋dest內(nèi)容 | 覆蓋dest內(nèi)容 |
Source種類 | 文件或目錄 | 只能是目錄 |
可移植性 | 一般(自行維護(hù)) | 強(qiáng)(docker托管) |
宿主直接訪問 | 容易(僅需chown) | 受限(需登陸root用戶)* |
*注釋:Docker無(wú)法簡(jiǎn)單地通過(guò)sudo chown someuser: -R /var/lib/docker/volumes/somevolume來(lái)將volume的內(nèi)容開放給主機(jī)上的普通用戶訪問,如果開放更多權(quán)限則有安全風(fēng)險(xiǎn)。而這點(diǎn)上Podman的設(shè)計(jì)就要理想得多,volume存放在$HOME/.local/share/containers/storage/volumes/路徑下,即提供了便捷性,又保障了安全性。無(wú)需root權(quán)限即可運(yùn)行容器,這正是Podman的優(yōu)勢(shì)之一,實(shí)際使用過(guò)程中的確受益良多。
創(chuàng)建bind mount時(shí)使用--volume和--mount的比較
對(duì)比項(xiàng) | --volume 或 -v | --mount type=bind |
---|---|---|
如果主機(jī)路徑不存在 | 自動(dòng)創(chuàng)建 | 命令報(bào)錯(cuò) |
官方文檔
DOCKER(1) ? ? ? ? ? ? ? ? ? ? ? ? ?JUNE 2014 ? ? ? ? ? ? ? ? ? ? ? ? DOCKER(1) NAME ? ? ? ?docker-run - Run a command in a new container SYNOPSIS ? ? ? ?docker run ? ? ? ?[--mount[=[MOUNT]]] ? ? ? ?[-v|--volume[=[[HOST-DIR:]CONTAINER-DIR[:OPTIONS]]]] ? ? ? ?IMAGE OPTIONS ? ? ? ?--mount type=TYPE,TYPE-SPECIFIC-OPTION[,...] ? ? ? ? ? Attach a filesystem mount to the container ? ? ? ?Current supported mount TYPES are bind, volume, and tmpfs. ? ? ? ?e.g. ? ? ? ?type=bind,source=/path/on/host,destination=/path/in/container ? ? ? ?type=volume,source=my-volume,destination=/path/in/container,volume-label="color=red",volume-label="shape=round" ? ? ? ?type=tmpfs,tmpfs-size=512M,destination=/path/in/container ? ? ? ?Common Options: ? ? ? ? ? ? ? · src, source: mount source spec for bind and volume. Mandatory ? ? ? ? ? ? ? ? for bind. ? ? ? ? ? ? ? · dst, destination, target: mount destination spec. ? ? ? ? ? ? ? · ro, readonly: true or false (default). ? ? ? ?Note: setting readonly for a bind mount does not make its submounts ? ? ? ? ? read-only on the current Linux implementation. See also ? ? ? ?bind-nonrecursive. ? ? ? ?Options specific to bind: ? ? ? ? ? ? ? · bind-propagation: shared, slave, private, rshared, rslave, or ? ? ? ? ? ? ? ? rprivate(default). See also mount(2). ? ? ? ? ? ? ? · consistency: consistent(default), cached, or delegated. ? ? ? ? ? ? ? ? Currently, only effective for Docker for Mac. ? ? ? ? ? ? ? · bind-nonrecursive: true or false (default). If set to true, ? ? ? ? ? ? ? ? submounts are not recursively bind-mounted. This option is ? ? ? ? ? ? ? ? useful for readonly bind mount. ? ? ? ?Options specific to volume: ? ? ? ? ? ? ? · volume-driver: Name of the volume-driver plugin. ? ? ? ? ? ? ? · volume-label: Custom metadata. ? ? ? ? ? ? ? · volume-nocopy: true(default) or false. If set to false, the ? ? ? ? ? ? ? ? Engine copies existing files and directories under the ? ? ? ? ? ? ? ? mount-path into the volume, allowing the host to access them. ? ? ? ? ? ? ? · volume-opt: specific to a given volume driver. ? ? ? ? ? ? ?? ? ? ? ?Options specific to tmpfs: ? ? ? ? ? ? ? · tmpfs-size: Size of the tmpfs mount in bytes. Unlimited by ? ? ? ? ? ? ? ? default in Linux. ? ? ? ? ? ? ? · tmpfs-mode: File mode of the tmpfs in octal. (e.g. 700 or ? ? ? ? ? ? ? ? 0700.) Defaults to 1777 in Linux. ? ? ? ?-v|--volume[=[[HOST-DIR:]CONTAINER-DIR[:OPTIONS]]] ? ? ? ? ? Create a bind mount. If you specify, -v /HOST-DIR:/CONTAINER-DIR, ? ? ? ?Docker ? ? ? ? ? bind mounts /HOST-DIR in the host to /CONTAINER-DIR in the Docker ? ? ? ? ? container. If 'HOST-DIR' is omitted, ?Docker automatically creates ? ? ? ?the new ? ? ? ? ? volume on the host. ?The OPTIONS are a comma delimited list and can ? ? ? ?be: ? ? ? ? ? ? ? · [rw|ro] ? ? ? ? ? ? ? · [z|Z] ? ? ? ? ? ? ? · [[r]shared|[r]slave|[r]private] ? ? ? ? ? ? ? · [delegated|cached|consistent] ? ? ? ? ? ? ? · [nocopy] ? ? ? ?The CONTAINER-DIR must be an absolute path such as /src/docs. The ? ? ? ?HOST-DIR can be an absolute path or a name value. A name value must ? ? ? ?start with an alphanumeric character, followed by a-z0-9, _ ? ? ? ?(underscore), . (period) or - (hyphen). An absolute path starts with a ? ? ? ?/ (forward slash). ? ? ? ?If you supply a HOST-DIR that is an absolute path, ?Docker bind-mounts ? ? ? ?to the path you specify. If you supply a name, Docker creates a named ? ? ? ?volume by that name. For example, you can specify either /foo or foo ? ? ? ?for a HOST-DIR value. If you supply the /foo value, Docker creates a ? ? ? ?bind mount. If you supply the foo specification, Docker creates a named ? ? ? ?volume. ? ? ? ?You can specify multiple ?-v options to mount one or more mounts to a ? ? ? ?container. To use these same mounts in other containers, specify the ? ? ? ?--volumes-from option also. ? ? ? ?You can supply additional options for each bind mount following an ? ? ? ?additional colon. ?A :ro or :rw suffix mounts a volume in read-only or ? ? ? ?read-write mode, respectively. By default, volumes are mounted in ? ? ? ?read-write mode. ?You can also specify the consistency requirement for ? ? ? ?the mount, either :consistent (the default), :cached, or :delegated. ? ? ? ?Multiple options are separated by commas, e.g. :ro,cached. ? ? ? ?Labeling systems like SELinux require that proper labels are placed on ? ? ? ?volume content mounted into a container. Without a label, the security ? ? ? ?system might prevent the processes running inside the container from ? ? ? ?using the content. By default, Docker does not change the labels set by ? ? ? ?the OS. ? ? ? ?To change a label in the container context, you can add either of two ? ? ? ?suffixes :z or :Z to the volume mount. These suffixes tell Docker to ? ? ? ?relabel file objects on the shared volumes. The z option tells Docker ? ? ? ?that two containers share the volume content. As a result, Docker ? ? ? ?labels the content with a shared content label. Shared volume labels ? ? ? ?allow all containers to read/write content. ?The Z option tells Docker ? ? ? ?to label the content with a private unshared label. ?Only the current ? ? ? ?container can use a private volume. ? ? ? ?By default bind mounted volumes are private. That means any mounts done ? ? ? ?inside container will not be visible on host and vice-a-versa. One can ? ? ? ?change this behavior by specifying a volume mount propagation property. ? ? ? ?Making a volume shared mounts done under that volume inside container ? ? ? ?will be visible on host and vice-a-versa. Making a volume slave enables ? ? ? ?only one way mount propagation and that is mounts done on host under ? ? ? ?that volume will be visible inside container but not the other way ? ? ? ?around. ? ? ? ?To control mount propagation property of volume one can use :[r]shared, ? ? ? ?:[r]slave or :[r]private propagation flag. Propagation property can be ? ? ? ?specified only for bind mounted volumes and not for internal volumes or ? ? ? ?named volumes. For mount propagation to work source mount point (mount ? ? ? ?point where source dir is mounted on) has to have right propagation ? ? ? ?properties. For shared volumes, source mount point has to be shared. ? ? ? ?And for slave volumes, source mount has to be either shared or slave. ? ? ? ?Use df <source-dir> to figure out the source mount and then use findmnt ? ? ? ?-o TARGET,PROPAGATION <source-mount-dir> to figure out propagation ? ? ? ?properties of source mount. If findmnt utility is not available, then ? ? ? ?one can look at mount entry for source mount point in ? ? ? ?/proc/self/mountinfo. Look at optional fields and see if any ? ? ? ?propagation properties are specified. ?shared:X means mount is shared, ? ? ? ?master:X means mount is slave and if nothing is there that means mount ? ? ? ?is private. ? ? ? ?To change propagation properties of a mount point use mount command. ? ? ? ?For example, if one wants to bind mount source directory /foo one can ? ? ? ?do mount --bind /foo /foo and mount --make-private --make-shared /foo. ? ? ? ?This will convert /foo into a shared mount point. Alternatively one can ? ? ? ?directly change propagation properties of source mount. Say / is source ? ? ? ?mount for /foo, then use mount --make-shared / to convert / into a ? ? ? ?shared mount. ? ? ? ? ? ? ? Note: When using systemd to manage the Docker daemon's start and ? ? ? ? ? ? ? stop, in the systemd unit file there is an option to control ? ? ? ? ? ? ? mount propagation for the Docker daemon itself, called ? ? ? ? ? ? ? MountFlags. The value of this setting may cause Docker to not ? ? ? ? ? ? ? see mount propagation changes made on the mount point. For ? ? ? ? ? ? ? example, if this value is slave, you may not be able to use the ? ? ? ? ? ? ? shared or rshared propagation on a volume. ? ? ? ?To disable automatic copying of data from the container path to the ? ? ? ?volume, use the nocopy flag. The nocopy flag can be set on bind mounts ? ? ? ?and named volumes. ? ? ? ?See also --mount, which is the successor of --tmpfs and --volume. ?Even ? ? ? ?though there is no plan to deprecate --volume, usage of --mount is ? ? ? ?recommended. Docker Community ? ? ? ? ? ? ?Docker User Manuals ? ? ? ? ? ? ? ? ? ?DOCKER(1)
到此這篇關(guān)于Docker數(shù)據(jù)卷掛載命令volume(-v)與mount的使用總結(jié)的文章就介紹到這了,更多相關(guān)Docker volume(-v)與mount內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- docker 啟動(dòng)elasticsearch鏡像,掛載目錄后報(bào)錯(cuò)的解決
- docker-修改容器掛載目錄的3種方法小結(jié)
- docker 查看容器的掛載目錄操作
- docker中容器數(shù)據(jù)卷volume介紹
- Docker容器中數(shù)據(jù)卷volumes的使用
- Docker容器數(shù)據(jù)卷介紹及操作示例
- Docker容器數(shù)據(jù)卷技術(shù)介紹
- Docker容器數(shù)據(jù)卷的使用教程
- Docker中容器數(shù)據(jù)卷詳解
- docker如何對(duì)已經(jīng)啟動(dòng)的容器添加目錄映射(掛載目錄)
- Docker數(shù)據(jù)卷和掛載目錄的使用
相關(guān)文章
使用docker?compose快速配置一組容器服務(wù)詳解
這篇文章主要為大家介紹了使用docker-?compose快速配置一組容器服務(wù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11關(guān)于dockerfile build過(guò)程中報(bào)/bin/sh: pip: command not found的解決方法
這篇文章主要介紹了關(guān)于dockerfile build過(guò)程中報(bào)/bin/sh: pip: command not found的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Docker向數(shù)據(jù)卷Volume寫入數(shù)據(jù)
這篇文章介紹了Docker向數(shù)據(jù)卷Volume寫入數(shù)據(jù)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03如何設(shè)置docker開機(jī)自啟動(dòng),并設(shè)置容器自動(dòng)重啟
這篇文章主要介紹了如何設(shè)置docker開機(jī)自啟動(dòng),并設(shè)置容器自動(dòng)重啟問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02k3d入門指南之在Docker中運(yùn)行K3s的詳細(xì)教程
在本文中,我們將簡(jiǎn)單了解k3d,這是一款可讓您在安裝了Docker的任何地方運(yùn)行一次性Kubernetes集群的工具,此外在本文中我們還將探討在使用k3d中可能會(huì)出現(xiàn)的一切問題,感興趣的朋友跟隨小編一起看看吧2021-05-05docker<容器數(shù)據(jù)卷-v>對(duì)容器內(nèi)數(shù)據(jù)持久化詳解(備份)
容器的數(shù)據(jù)持久化主要是指宿主機(jī)與容器,以及容器與容器之間進(jìn)行數(shù)據(jù)交互,下面這篇文章主要給大家介紹了關(guān)于docker<容器數(shù)據(jù)卷-v>對(duì)容器內(nèi)數(shù)據(jù)持久化的相關(guān)資料,需要的朋友可以參考下2023-03-03