简体   繁体   English

在 docker 容器上安装多个卷?

[英]Mounting multiple volumes on a docker container?

I know I can mount a directory in my host on my container using something like我知道我可以使用类似的东西在我的容器上的主机中挂载一个目录

docker run -t -i -v '/on/my/host:/on/the/container' ubuntu /bin/bash

Is there a way to create more than one host-container pair?有没有办法创建多个主机-容器对? eg a comma-separated list, or pass in an array?例如逗号分隔的列表,或传入一个数组?

Pass multiple -v arguments.传递多个-v参数。

For instance:例如:

docker -v /on/my/host/1:/on/the/container/1 \
       -v /on/my/host/2:/on/the/container/2 \
       ...

Docker now recommends migrating towards using --mount . Docker 现在建议迁移到使用--mount

Multiple volume mounts are also explained in detail in the current Docker documentation.当前的 Docker 文档中也详细解释了多个卷挂载。

From: https://docs.docker.com/storage/bind-mounts/来自: https ://docs.docker.com/storage/bind-mounts/

$ docker run -d \
  -it \
  --name devtest \
  --mount type=bind,source="$(pwd)"/target,target=/app \
  --mount type=bind,source="$(pwd)"/target,target=/app2,readonly,bind-propagation=rslave \
  nginx:latest

Original older answer should still work;原来的旧答案应该仍然有效; just trying to keep the answer aligned to current best known method.只是试图使答案与当前最知名的方法保持一致。

You can use -v option multiple times in docker run command to mount multiple directory in container:您可以在docker run命令中多次使用-v选项在容器中挂载多个目录:

docker run -t -i \
  -v '/on/my/host/test1:/on/the/container/test1' \
  -v '/on/my/host/test2:/on/the/container/test2' \
  ubuntu /bin/bash

You can have Read only or Read and Write only on the volume您可以在卷上设置只读或只读和只写

docker -v /on/my/host/1:/on/the/container/1:ro \

docker -v /on/my/host/2:/on/the/container/2:rw \

On Windows: if you had to mount two directories E:\data\dev & E:\data\dev2在 Windows 上:如果必须挂载两个目录 E:\data\dev & E:\data\dev2

Use:利用:

docker run -v E:\data\dev:c:/downloads -v E:\data\dev2 c:/downloads2 -i --publish 1111:80 -P SomeBuiltContainerName:SomeLabel

I saw a comment asking if read-write or read-only was the default option;我看到一条评论询问读写或只读是默认选项; read-write is the default option.读写是默认选项。 (making a post because I haven't enough rep to comment) (因为我没有足够的代表发表评论而发帖)

Per docker documentation , running the following:根据docker 文档,运行以下命令:

docker run -d \
--name devtest \
--mount source=myvol2,target=/app \
nginx:latest

and then using docker inspect devtest and locating the "RW" option in the "Mounts" section of the output:然后使用docker inspect devtest并在输出的“Mounts”部分中找到“RW”选项:

"Mounts": [
    {
        "Type": "volume",
        "Source": "/var/lib/docker/volumes/myvol2/_data",
        "Destination": "/app",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],

lets us see that the default option lets the volume be both read- and write-able.让我们看到默认选项让卷既可读又可写。


To set your volume as read-only (again perofficial documentation ), add readonly after your source and destination tags:要将您的卷设置为只读(同样根据官方文档),请在source标签和destination标签之后添加readonly

docker run -d \
--name=nginxtest \
--mount source=nginx-vol,destination=/usr/share/nginx/html,readonly \
nginx:latest

Run docker inspect nginxtest and locate "Mounts":运行docker inspect nginxtest并找到“Mounts”:

"Mounts": [
    {
        "Type": "volume",
        "Source": "/var/lib/docker/volumes/nginx-vol/_data",
        "Destination": "/usr/share/nginx/html",
        "Driver": "local",
        "Mode": "",
        "RW": false,
        "Propagation": ""
    }
],

(Note: I don't know why official documentation swaps between target and destination tags, but I am working under the assumption they can be used interchangeably.) (注意:我不知道为什么官方文档会在target标签和destination标签之间交换,但我假设它们可以互换使用。)

For a runable example:对于一个可运行的例子:

docker run -v /Users/brandomiranda/iit-term-synthesis:/home/bot/iit-term-synthesis \
           -v /Users/brandomiranda/pycoq:/home/bot/pycoq \
           -v /Users/brandomiranda/ultimate-utils:/home/bot/ultimate-utils \
           -ti brandojazz/iit-term-synthesis:test bash

but first do:但首先要做:

docker pull brandojazz/iit-term-synthesis:test

或者你可以做

docker run -v /var/volume1 -v /var/volume2 DATA busybox true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM