本文翻譯自docker官網(wǎng):https://docs.docker.com/storage/volumes/
Use volumes
Volumes are the preferred mechanism for persisting data generated by and used
by Docker containers. While bind mounts are dependent on the
directory structure and OS of the host machine, volumes are completely managed by
Docker. Volumes have several advantages over bind mounts:
卷是保存Docker容器生成和使用的數(shù)據(jù)的首選機(jī)制。綁定裝載依賴(lài)于主機(jī)的目錄結(jié)構(gòu)和操作系統(tǒng),卷完全由Docker管理。
與綁定裝載相比,卷有幾個(gè)優(yōu)點(diǎn):
- Volumes are easier to back up or migrate than bind mounts.
卷比綁定裝載更易于備份或遷移。
- You can manage volumes using Docker CLI commands or the Docker API.
您可以使用Docker CLI命令或Docker API管理卷。
- Volumes work on both Linux and Windows containers.
卷可以在Linux和Windows容器上工作。
- Volumes can be more safely shared among multiple containers.
卷可以在多個(gè)容器之間更安全地共享。
- Volume drivers let you store volumes on remote hosts or cloud providers, to
encrypt the contents of volumes, or to add other functionality.卷驅(qū)動(dòng)程序允許您在遠(yuǎn)程主機(jī)或云提供商上存儲(chǔ)卷,以加密卷的內(nèi)容或添加其他功能。
- New volumes can have their content pre-populated by a container.
新卷的內(nèi)容可以由容器預(yù)先填充。
- Volumes on Docker Desktop have much higher performance than bind mounts from
Mac and Windows hosts.Docker Desktop上的卷比Mac和Windows主機(jī)上的綁定裝載具有更高的性能。
In addition, volumes are often a better choice than persisting data in a
container's writable layer, because a volume does not increase the size of the
containers using it, and the volume's contents exist outside the lifecycle of a
given container.
此外,與將數(shù)據(jù)持久化到容器的可寫(xiě)層相比,卷通常是更好的選擇,
因?yàn)榫聿粫?huì)增加使用它的容器的大小,而且卷的內(nèi)容存在于給定容器的生命周期之外。

If your container generates non-persistent state data, consider using a
tmpfs mount to avoid storing the data anywhere permanently, and to
increase the container's performance by avoiding writing into the container's
writable layer.
如果容器生成非持久狀態(tài)數(shù)據(jù),請(qǐng)考慮使用tmpfs裝載以避免將數(shù)據(jù)永久存儲(chǔ)在任何位置,并通過(guò)避免寫(xiě)入容器的可寫(xiě)層來(lái)提高容器的性能。
Volumes use rprivate bind propagation, and bind propagation is not
configurable for volumes.
卷使用rprivate綁定傳播,并且不能為卷配置綁定傳播。???
Choose the -v or --mount flag
In general, --mount is more explicit and verbose. The biggest difference is that
the -v syntax combines all the options together in one field, while the --mount
syntax separates them. Here is a comparison of the syntax for each flag.
一般來(lái)說(shuō),
-mount更顯式和冗長(zhǎng)。最大的區(qū)別是-v語(yǔ)法將所有選項(xiàng)組合在一個(gè)字段中,
而-mount語(yǔ)法將它們分開(kāi)。下面是每個(gè)標(biāo)志的語(yǔ)法比較。
If you need to specify volume driver options, you must use --mount.
如果需要指定卷驅(qū)動(dòng)程序選項(xiàng),則必須使用
--mount。
-
-vor--volume: Consists of three fields, separated by colon characters
(:). The fields must be in the correct order, and the meaning of each field
is not immediately obvious.由三個(gè)字段組成,用冒號(hào)字符(:)分隔。字段的順序必須正確,并且每個(gè)字段的含義不是很明顯。
- In the case of named volumes, the first field is the name of the volume, and is
unique on a given host machine. For anonymous volumes, the first field is
omitted.對(duì)于命名卷,第一個(gè)字段是卷的名稱(chēng),并且在給定的主機(jī)上是唯一的。對(duì)于匿名卷,省略第一個(gè)字段。
- The second field is the path where the file or directory are mounted in
the container.第二個(gè)字段是在容器中裝入文件或目錄的路徑。
- The third field is optional, and is a comma-separated list of options, such
asro. These options are discussed below.第三個(gè)字段是可選的,是以逗號(hào)分隔的選項(xiàng)列表,例如ro。下面討論這些選項(xiàng)
- In the case of named volumes, the first field is the name of the volume, and is
-
--mount: Consists of multiple key-value pairs, separated by commas and each
consisting of a<key>=<value>tuple. The--mountsyntax is more verbose
than-vor--volume, but the order of the keys is not significant, and
the value of the flag is easier to understand.由多個(gè)鍵值對(duì)組成,用逗號(hào)分隔,每個(gè)鍵值對(duì)由一個(gè)
<key>=<value>元組組成。
--mount語(yǔ)法比-v或--volume更詳細(xì),但是鍵的順序并不重要,而且標(biāo)志的值更容易理解。- The
typeof the mount, which can bebind,volume, or
tmpfs. This topic discusses volumes, so the type is always
volume.裝載的類(lèi)型,可以是
bind、volume或tmpfs。本主題討論volumes,因此類(lèi)型始終為volumes。 - The
sourceof the mount. For named volumes, this is the name of the volume.
For anonymous volumes, this field is omitted. May be specified assource
orsrc.裝載的源
source。對(duì)于命名卷,這是卷的名稱(chēng)。對(duì)于匿名卷,省略此字段??梢灾付?code>source或src。 - The
destinationtakes as its value the path where the file or directory
is mounted in the container. May be specified asdestination,dst,
ortarget.destination(目標(biāo))將文件或目錄裝入容器的路徑作為其值??梢灾付?code>destination、dst或target。 - The
readonlyoption, if present, causes the bind mount to be mounted into
the container as read-only.readonly選項(xiàng)(如果存在)將導(dǎo)致綁定裝載以只讀方式裝載到容器中。 - The
volume-optoption, which can be specified more than once, takes a
key-value pair consisting of the option name and its value.volume-opt選項(xiàng)可以多次指定,它采用由選項(xiàng)名及其值組成的鍵值對(duì)。
- The
Escape values from outer CSV parser
從外部CSV解析器轉(zhuǎn)義值If your volume driver accepts a comma-separated list as an option,
you must escape the value from the outer CSV parser. To escape avolume-opt,
surround it with double quotes (") and surround the entire mount parameter
with single quotes (').
如果卷驅(qū)動(dòng)程序接受逗號(hào)分隔列表作為選項(xiàng),則必須從外部CSV解析器轉(zhuǎn)義該值。
要轉(zhuǎn)義volume-opt,請(qǐng)用雙引號(hào)(")將其括起來(lái),并用單引號(hào) (')將整個(gè)mount參數(shù)括起來(lái)。For example, the
localdriver accepts mount options as a comma-separated
list in theoparameter. This example shows the correct way to escape the list.
例如,本地驅(qū)動(dòng)程序在o參數(shù)中接受以逗號(hào)分隔的列表形式的裝載選項(xiàng)。此示例顯示了轉(zhuǎn)義列表的正確方法。$ docker service create \ --mount 'type=volume,src=<VOLUME-NAME>,dst=<CONTAINER-PATH>,volume-driver=local,volume-opt=type=nfs,volume-opt=device=<nfs-server>:<nfs-path>,"volume-opt=o=addr=<nfs-address>,vers=4,soft,timeo=180,bg,tcp,rw"' --name myservice \ <IMAGE>{: .warning}
The examples below show both the --mount and -v syntax where possible, and
--mount is presented first.
下面的示例在可能的情況下顯示了
--mount和-v語(yǔ)法,并且--mount是首先顯示的。
Differences between -v and --mount behavior
-v和--mount行為之間的差異
As opposed to bind mounts, all options for volumes are available for both
--mount and -v flags.
與綁定裝載不同,卷的所有選項(xiàng)都可用于
--mount和-v標(biāo)志。
When using volumes with services, only --mount is supported.
將卷與服務(wù)一起使用時(shí),只支持
--mount。
Create and manage volumes
Unlike a bind mount, you can create and manage volumes outside the scope of any
container.
與綁定裝載不同,您可以在任何容器的作用域之外創(chuàng)建和管理卷。
Create a volume:
$ docker volume create my-vol
List volumes:
$ docker volume ls
local my-vol
Inspect a volume:
$ docker volume inspect my-vol
[
{
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
"Name": "my-vol",
"Options": {},
"Scope": "local"
}
]
Remove a volume:
$ docker volume rm my-vol
Start a container with a volume
攜帶卷啟動(dòng)容器
If you start a container with a volume that does not yet exist, Docker creates
the volume for you. The following example mounts the volume myvol2 into
/app/ in the container.
如果啟動(dòng)的容器中有一個(gè)尚不存在的卷,Docker將為您創(chuàng)建該卷。下面的示例將卷
myvol2裝載到容器中的/app/中。
The -v and --mount examples below produce the same result. You can't run
them both unless you remove the devtest container and the myvol2 volume
after running the first one.
下面的
-v和--mount示例產(chǎn)生相同的結(jié)果。除非在運(yùn)行第一個(gè)容器之后刪除devtest容器和myvol2卷,否則不能同時(shí)運(yùn)行它們。
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" data-group="mount" data-target="#mount-run"><code>--mount</code></a></li>
<li><a data-toggle="tab" data-group="volume" data-target="#v-run"><code>-v</code></a></li>
</ul>
<div class="tab-content">
<div id="mount-run" class="tab-pane fade in active" markdown="1">
$ docker run -d \
--name devtest \
--mount source=myvol2,target=/app \
nginx:latest
</div>
<div id="v-run" class="tab-pane fade" markdown="1">
$ docker run -d \
--name devtest \
-v myvol2:/app \
nginx:latest
</div>
</div>
Use docker inspect devtest to verify that the volume was created and mounted
correctly. Look for the Mounts section:
使用
docker inspect devtest驗(yàn)證卷是否已正確創(chuàng)建和裝入。查找Mounts部分:
"Mounts": [
{
"Type": "volume",
"Name": "myvol2",
"Source": "/var/lib/docker/volumes/myvol2/_data",
"Destination": "/app",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
This shows that the mount is a volume, it shows the correct source and
destination, and that the mount is read-write.
這表明裝載是一個(gè)卷,它顯示了正確的源和目標(biāo),并且裝載是讀寫(xiě)的。
Stop the container and remove the volume. Note volume removal is a separate
step.
停止容器并移除卷。注意:卷刪除是一個(gè)單獨(dú)的步驟。
$ docker container stop devtest
$ docker container rm devtest
$ docker volume rm myvol2
Use a volume with docker-compose
通過(guò)docker-compose使用卷
A single docker compose service with a volume looks like this:
具有卷的單個(gè)docker compose服務(wù)如下所示:
version: "{{ site.compose_file_v3 }}"
services:
frontend:
image: node:lts
volumes:
- myapp:/home/node/app
volumes:
myapp:
On the first invocation of docker-compose up the volume will be created. The same
volume will be reused on following invocations.
在第一次調(diào)用
docker-compose up時(shí),將創(chuàng)建卷。相同的卷將在以下調(diào)用中重用。
A volume may be created directly outside of compose with docker volume create and
then referenced inside docker-compose.yml as follows:
卷可以直接在compose外部通過(guò)
docker volume create創(chuàng)建,然后在docker內(nèi)部引用docker-compose.yml` 具體如下:
version: "{{ site.compose_file_v3 }}"
services:
frontend:
image: node:lts
volumes:
- myapp:/home/node/app
volumes:
myapp:
external: true
For more information about using volumes with compose see
the compose reference.
通過(guò)docker-compose使用卷的更多詳情請(qǐng)參考the compose reference
Start a service with volumes
When you start a service and define a volume, each service container uses its own
local volume. None of the containers can share this data if you use the local
volume driver, but some volume drivers do support shared storage. Docker for AWS and
Docker for Azure both support persistent storage using the Cloudstor plugin.
啟動(dòng)服務(wù)并定義卷時(shí),每個(gè)服務(wù)容器都使用自己的本地卷。
如果使用本地卷驅(qū)動(dòng)程序,則所有容器都無(wú)法共享此數(shù)據(jù),但某些卷驅(qū)動(dòng)程序確實(shí)支持共享存儲(chǔ)。
AWS的Docker和Azure的Docker都支持使用Cloudstor插件的持久存儲(chǔ)。
The following example starts a nginx service with four replicas, each of which
uses a local volume called myvol2.
下面的示例使用四個(gè)副本啟動(dòng)
nginx服務(wù),每個(gè)副本使用一個(gè)名為myvol2的本地卷。
$ docker service create -d \
--replicas=4 \
--name devtest-service \
--mount source=myvol2,target=/app \
nginx:latest
Use docker service ps devtest-service to verify that the service is running:
使用docker service ps devtest service驗(yàn)證服務(wù)是否正在運(yùn)行:
$ docker service ps devtest-service
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
4d7oz1j85wwn devtest-service.1 nginx:latest moby Running Running 14 seconds ago
Remove the service, which stops all its tasks:
刪除服務(wù),該服務(wù)將停止其所有任務(wù):
$ docker service rm devtest-service
Removing the service does not remove any volumes created by the service.
Volume removal is a separate step.
刪除該服務(wù)不會(huì)刪除該服務(wù)創(chuàng)建的任何卷。卷刪除是一個(gè)單獨(dú)的步驟。
Syntax differences for services
服務(wù)的語(yǔ)法差異
The docker service create command does not support the -v or --volume flag.
When mounting a volume into a service's containers, you must use the --mount
flag.
docker service create命令不支持-v或--volume標(biāo)志。將卷裝入服務(wù)容器時(shí),必須使用--mount標(biāo)志。
Populate a volume using a container
使用容器填充卷
If you start a container which creates a new volume, as above, and the container
has files or directories in the directory to be mounted (such as /app/ above),
the directory's contents are copied into the volume. The container then
mounts and uses the volume, and other containers which use the volume also
have access to the pre-populated content.
如果啟動(dòng)一個(gè)創(chuàng)建新卷的容器(如上所述),并且該容器在要裝入的目錄(如上
/app/)中有文件或目錄,
則該目錄的內(nèi)容將復(fù)制到卷中。然后容器裝載并使用該卷,使用該卷的其他容器也可以訪問(wèn)預(yù)填充的內(nèi)容。
To illustrate this, this example starts an nginx container and populates the
new volume nginx-vol with the contents of the container's
/usr/share/nginx/html directory, which is where Nginx stores its default HTML
content.
為了說(shuō)明這一點(diǎn),本例啟動(dòng)一個(gè)
nginx容器,
并用容器的/usr/share/nginx/html目錄的內(nèi)容填充新的卷nginx-vol,nginx在其中存儲(chǔ)其默認(rèn)的html內(nèi)容。
The --mount and -v examples have the same end result.
--mount和-v示例具有相同的最終結(jié)果。
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" data-group="mount" data-target="#mount-empty-run"><code>--mount</code></a></li>
<li><a data-toggle="tab" data-group="volume" data-target="#v-empty-run"><code>-v</code></a></li>
</ul>
<div class="tab-content">
<div id="mount-empty-run" class="tab-pane fade in active" markdown="1">
$ docker run -d \
--name=nginxtest \
--mount source=nginx-vol,destination=/usr/share/nginx/html \
nginx:latest
</div>
<div id="v-empty-run" class="tab-pane fade" markdown="1">
$ docker run -d \
--name=nginxtest \
-v nginx-vol:/usr/share/nginx/html \
nginx:latest
</div>
</div>
After running either of these examples, run the following commands to clean up
the containers and volumes. Note volume removal is a separate step.
在運(yùn)行這些示例中的任何一個(gè)之后,運(yùn)行以下命令來(lái)清理容器和卷。注:卷刪除是一個(gè)單獨(dú)的步驟。
$ docker container stop nginxtest
$ docker container rm nginxtest
$ docker volume rm nginx-vol
Use a read-only volume
使用只讀卷
For some development applications, the container needs to write into the bind
mount so that changes are propagated back to the Docker host. At other times,
the container only needs read access to the data. Remember that multiple
containers can mount the same volume, and it can be mounted read-write for some
of them and read-only for others, at the same time.
對(duì)于某些開(kāi)發(fā)應(yīng)用程序,容器需要寫(xiě)入綁定裝載,以便將更改傳播回Docker主機(jī)。
在其他時(shí)候,容器只需要對(duì)數(shù)據(jù)進(jìn)行讀訪問(wèn)。請(qǐng)記住,多個(gè)容器可以裝載同一個(gè)卷,
其中一些容器可以以讀寫(xiě)方式裝載,其他容器可以以只讀方式同時(shí)裝載。
This example modifies the one above but mounts the directory as a read-only
volume, by adding ro to the (empty by default) list of options, after the
mount point within the container. Where multiple options are present, separate
them by commas.
此示例修改了上面的示例,但通過(guò)將
ro添加到(默認(rèn)情況下為空)選項(xiàng)列表中,在容器內(nèi)的裝入點(diǎn)之后,將
目錄作為只讀卷裝入。如果存在多個(gè)選項(xiàng),請(qǐng)用逗號(hào)分隔它們。
The --mount and -v examples have the same result.
--mount和-v示例具有相同的最終結(jié)果。
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" data-group="mount" data-target="#mount-readonly"><code>--mount</code></a></li>
<li><a data-toggle="tab" data-group="volume" data-target="#v-readonly"><code>-v</code></a></li>
</ul>
<div class="tab-content">
<div id="mount-readonly" class="tab-pane fade in active" markdown="1">
$ docker run -d \
--name=nginxtest \
--mount source=nginx-vol,destination=/usr/share/nginx/html,readonly \
nginx:latest
</div>
<div id="v-readonly" class="tab-pane fade" markdown="1">
$ docker run -d \
--name=nginxtest \
-v nginx-vol:/usr/share/nginx/html:ro \
nginx:latest
</div>
</div>
Use docker inspect nginxtest to verify that the readonly mount was created
correctly. Look for the Mounts section:
使用
docker inspect nginxtest驗(yàn)證是否正確創(chuàng)建了只讀裝載。查找Mounts部分:
"Mounts": [
{
"Type": "volume",
"Name": "nginx-vol",
"Source": "/var/lib/docker/volumes/nginx-vol/_data",
"Destination": "/usr/share/nginx/html",
"Driver": "local",
"Mode": "",
"RW": false,
"Propagation": ""
}
],
Stop and remove the container, and remove the volume. Volume removal is a
separate step.
停止并移除容器,然后移除卷。卷刪除是一個(gè)單獨(dú)的步驟。
$ docker container stop nginxtest
$ docker container rm nginxtest
$ docker volume rm nginx-vol
Share data among machines
在機(jī)器之間共享數(shù)據(jù)
When building fault-tolerant applications, you might need to configure multiple
replicas of the same service to have access to the same files.
在構(gòu)建容錯(cuò)應(yīng)用程序時(shí),可能需要配置同一服務(wù)的多個(gè)副本以訪問(wèn)相同的文件。
[圖片上傳失敗...(image-825de1-1620140991461)]
There are several ways to achieve this when developing your applications.
One is to add logic to your application to store files on a cloud object
storage system like Amazon S3. Another is to create volumes with a driver that
supports writing files to an external storage system like NFS or Amazon S3.
在開(kāi)發(fā)應(yīng)用程序時(shí),有幾種方法可以實(shí)現(xiàn)這一點(diǎn)。一種是向應(yīng)用程序添加邏輯,將文件存儲(chǔ)在云對(duì)象存儲(chǔ)系統(tǒng)(如amazons3)上。
另一種方法是使用支持將文件寫(xiě)入NFS或amazons3等外部存儲(chǔ)系統(tǒng)的驅(qū)動(dòng)程序創(chuàng)建卷。
Volume drivers allow you to abstract the underlying storage system from the
application logic. For example, if your services use a volume with an NFS
driver, you can update the services to use a different driver, as an example to
store data in the cloud, without changing the application logic.
卷驅(qū)動(dòng)程序允許您從應(yīng)用程序邏輯中抽象底層存儲(chǔ)系統(tǒng)。例如,如果您的服務(wù)使用帶有NFS驅(qū)動(dòng)程序的卷,
您可以更新服務(wù)以使用不同的驅(qū)動(dòng)程序,例如在云中存儲(chǔ)數(shù)據(jù),而無(wú)需更改應(yīng)用程序邏輯。
Use a volume driver
使用卷驅(qū)動(dòng)程序
When you create a volume using docker volume create, or when you start a
container which uses a not-yet-created volume, you can specify a volume driver.
The following examples use the vieux/sshfs volume driver, first when creating
a standalone volume, and then when starting a container which creates a new
volume.
當(dāng)你使用
docker volume create創(chuàng)建卷時(shí),或啟動(dòng)使用尚未創(chuàng)建的卷的容器時(shí),可以指定卷驅(qū)動(dòng)程序。
以下示例首先在創(chuàng)建獨(dú)立卷時(shí)使用vieux/sshfs卷驅(qū)動(dòng)程序,然后在啟動(dòng)創(chuàng)建新卷的容器時(shí)使用。
Initial set-up
初始化設(shè)置
This example assumes that you have two nodes, the first of which is a Docker
host and can connect to the second using SSH.
本例假設(shè)你有兩個(gè)節(jié)點(diǎn),第一個(gè)節(jié)點(diǎn)是Docker主機(jī),可以使用SSH連接到第二個(gè)節(jié)點(diǎn)。
On the Docker host, install the vieux/sshfs plugin:
在Docker主機(jī)上,安裝
vieux/sshfs插件:
$ docker plugin install --grant-all-permissions vieux/sshfs
Create a volume using a volume driver
使用卷驅(qū)動(dòng)程序創(chuàng)建一個(gè)卷
This example specifies a SSH password, but if the two hosts have shared keys
configured, you can omit the password. Each volume driver may have zero or more
configurable options, each of which is specified using an -o flag.
本例指定了SSH密碼,但是如果兩個(gè)主機(jī)配置了共享密鑰,則可以省略該密碼。
每個(gè)卷驅(qū)動(dòng)程序可能有零個(gè)或多個(gè)可配置選項(xiàng),每個(gè)選項(xiàng)都使用-o標(biāo)志指定。
$ docker volume create --driver vieux/sshfs \
-o sshcmd=test@node2:/home/test \
-o password=testpassword \
sshvolume
Start a container which creates a volume using a volume driver
啟動(dòng)使用卷驅(qū)動(dòng)程序創(chuàng)建卷的容器
This example specifies a SSH password, but if the two hosts have shared keys
configured, you can omit the password. Each volume driver may have zero or more
configurable options. If the volume driver requires you to pass options, you
must use the --mount flag to mount the volume, rather than -v.
本例指定了SSH密碼,但是如果兩個(gè)主機(jī)配置了共享密鑰,則可以省略該密碼。
每個(gè)卷驅(qū)動(dòng)程序可能有零個(gè)或多個(gè)可配置選項(xiàng)。
如果卷驅(qū)動(dòng)程序要求您傳遞選項(xiàng),則必須使用--mount標(biāo)志來(lái)裝載卷,而不是-v。
$ docker run -d \
--name sshfs-container \
--volume-driver vieux/sshfs \
--mount src=sshvolume,target=/app,volume-opt=sshcmd=test@node2:/home/test,volume-opt=password=testpassword \
nginx:latest
Create a service which creates an NFS volume
創(chuàng)建一個(gè)其創(chuàng)建了NFS卷的服務(wù)
This example shows how you can create an NFS volume when creating a service.
This example uses 10.0.0.10 as the NFS server
and /var/docker-nfs as the exported directory on the NFS server.
Note that the volume driver specified is local.
此示例演示如何在創(chuàng)建服務(wù)時(shí)創(chuàng)建NFS卷。本例使用
10.0.0.10作為NFS服務(wù)器,
/var/docker-nfs作為NFS服務(wù)器上的導(dǎo)出目錄。請(qǐng)注意,指定的卷驅(qū)動(dòng)程序是local。
NFSv3
$ docker service create -d \
--name nfs-service \
--mount 'type=volume,source=nfsvolume,target=/app,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/docker-nfs,volume-opt=o=addr=10.0.0.10' \
nginx:latest
NFSv4
docker service create -d \
--name nfs-service \
--mount 'type=volume,source=nfsvolume,target=/app,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/docker-nfs,"volume-opt=o=addr=10.0.0.10,rw,nfsvers=4,async"' \
nginx:latest
Create CIFS/Samba volumes
You can mount a Samba share directly in docker without configuring a mount point on your host.
您可以直接在docker中裝載Samba共享,而無(wú)需在主機(jī)上配置裝載點(diǎn)。
docker volume create \
--driver local \
--opt type=cifs \
--opt device=//uxxxxx.your-server.de/backup \
--opt o=addr=uxxxxx.your-server.de,username=uxxxxxxx,password=*****,file_mode=0777,dir_mode=0777 \
--name cif-volume
Notice the addr option is required if using a hostname instead of an IP so docker can perform the hostname lookup.
請(qǐng)注意,如果使用主機(jī)名而不是IP,則需要
addr選項(xiàng),以便docker可以執(zhí)行主機(jī)名查找。
Backup, restore, or migrate data volumes
備份、恢復(fù)或遷移數(shù)據(jù)卷
Volumes are useful for backups, restores, and migrations. Use the
--volumes-from flag to create a new container that mounts that volume.
卷對(duì)于備份、恢復(fù)和遷移非常有用。使用
--volumes from標(biāo)志創(chuàng)建一個(gè)裝載該卷的新容器。
Backup a container
For example, create a new container named dbstore:
例如,創(chuàng)建一個(gè)名為
dbstore的新容器:
$ docker run -v /dbdata --name dbstore ubuntu /bin/bash
Then in the next command, we:
然后在下一個(gè)命令中,我們:
- Launch a new container and mount the volume from the
dbstorecontainer啟動(dòng)一個(gè)新容器并從
dbstore容器裝載卷 - Mount a local host directory as
/backup將本地主機(jī)目錄裝載為
/backup - Pass a command that tars the contents of the
dbdatavolume to abackup.tarfile inside our/backupdirectory.傳遞一個(gè)命令來(lái)打包
dbdata卷的內(nèi)容到一個(gè)backup.tar文件到我們的/backup目錄
$ docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
When the command completes and the container stops, we are left with a backup of
our dbdata volume.
當(dāng)命令完成并且容器停止時(shí),我們留下了
dbdata卷的備份。
Restore container from backup
從備份還原容器
With the backup just created, you can restore it to the same container, or
another that you made elsewhere.
使用剛剛創(chuàng)建的備份,您可以將其還原到同一個(gè)容器,或其他地方創(chuàng)建的容器。
For example, create a new container named dbstore2:
例如,創(chuàng)建一個(gè)名為
dbstore2的新容器:
$ docker run -v /dbdata --name dbstore2 ubuntu /bin/bash
Then un-tar the backup file in the new container`s data volume:
然后解壓縮備份文件到一個(gè)新容器的數(shù)據(jù)卷:
$ docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c "cd /dbdata && tar xvf /backup/backup.tar --strip 1"
You can use the techniques above to automate backup, migration and restore
testing using your preferred tools.
你可以使用上述技術(shù),使用你喜歡的工具自動(dòng)化備份、遷移和恢復(fù)測(cè)試。
Remove volumes
A Docker data volume persists after a container is deleted. There are two types
of volumes to consider:
刪除容器后,Docker數(shù)據(jù)卷仍然存在。有兩種類(lèi)型的卷需要考慮:
-
Named volumes have a specific source from outside the container, for example
awesome:/bar.命名卷具有來(lái)自容器外部的特定源,例如
awesome:/bar。 -
Anonymous volumes have no specific source so when the container is deleted, instruct the Docker Engine daemon to remove them.
匿名卷沒(méi)有特定的源,因此在刪除容器時(shí),請(qǐng)指示Docker引擎守護(hù)程序刪除它們。
Remove anonymous volumes
刪除匿名卷
To automatically remove anonymous volumes, use the --rm option. For example,
this command creates an anonymous /foo volume. When the container is removed,
the Docker Engine removes the /foo volume but not the awesome volume.
要自動(dòng)刪除匿名卷,請(qǐng)使用
--rm選項(xiàng)。例如,此命令創(chuàng)建匿名卷/foo。
當(dāng)容器被移除時(shí),Docker引擎移除/foo卷而不是awesome卷。
$ docker run --rm -v /foo -v awesome:/bar busybox top
Remove all volumes
To remove all unused volumes and free up space:
要?jiǎng)h除所有未使用的卷并釋放空間,請(qǐng)執(zhí)行以下操作:
$ docker volume prune
Next steps
- Learn about bind mounts.
- Learn about tmpfs mounts.
- Learn about storage drivers.
- Learn about third-party volume driver plugins.s