问题描述
我想知道我是否可以通过许多docker容器使用docker卷。 如果是这样,泊坞窗卷是否已锁定?
解决方法
您可以先创建一个命名卷,然后在一个或多个泊坞窗中使用它。
在创建命名卷(例如称为myvolume
)时,如果未指定驱动程序选项,则使用local
。因此,docker在/var/lib/docker/volumes
中创建一个文件夹。您的数据将保留在/var/lib/docker/volumes/myvolume/_data
尽管如此,这仅仅是信息,您不需要管理这些信息。您只需使用以下内容进行创建:
docker volume create myvolume
然后,使用卷名作为源。
docker run -v myvolume:/yourdestinationpath ...
如果使用docker compose,语法是相同的:
services:
myservice:
...
volumes:
- myvolume:/yourdestinationpath
关键是您没有使用绑定卷,而是在其中将要挂载的具体路径指定为源,而将docker卷名指定为源。
,这是一个最小的示例,其中我将本地目录装入容器中。我的本地目录将充当持久性卷,我可以在该持久性卷上从容器中进行读写。
我当前的目录包含一个input-file
(此文件应由容器读取)
容器cat
s input-file
的内容,并将其附加到output-file
(在这里,我是在伪造您的转换)
// The initial wiorking directory content
.
└── input-file
// Read the `input-file` and append its content to the `output-file` (both files are persisted on the host machine)
docker run -v $(pwd):/root/some-path ubuntu /bin/bash -c "cat /root/some-path/input-file >> /root/some-path/output-file"
// The outcome
.
├── input-file
└── output-file