I am using an image that has a VOLUME command, and I'd like to know how to share it with my host. I have done it using the -v command above, but I didn't know if I needed both the -v and VOLUME.推荐答案VOLUME 命令将在您的容器内挂载一个目录,并将在该目录内创建或编辑的任何文件存储在您的主机磁盘上在容器文件结构之外,绕过联合文件系统.The VOLUME command will mount a directory inside your container and store any files created or edited inside that directory on your hosts disk outside the container file structure, bypassing the union file system.这个想法是你的卷可以在你的 docker 容器之间共享,只要有一个容器(正在运行或停止)引用它们,它们就会一直存在.The idea is that your volumes can be shared between your docker containers and they will stay around as long as there's a container (running or stopped) that references them.您可以在运行容器时使用 --volumes-from 命令让其他容器挂载现有卷(在容器之间有效地共享它们).You can have other containers mount existing volumes (effectively sharing them between containers) by using the --volumes-from command when you run a container.VOLUME 和 -v 之间的根本区别在于:-v 将在 docker 容器内挂载操作系统中的现有文件,并且VOLUME 将在您的主机上创建一个新的空卷并将其安装在您的容器中.The fundamental difference between VOLUME and -v is this: -v will mount existing files from your operating system inside your docker container and VOLUME will create a new, empty volume on your host and mount it inside your container.示例:您有一个 Dockerfile,它定义了一个 VOLUME/var/lib/mysql.您构建 docker 镜像并标记它some-volume你运行容器然后,您有另一个 docker 镜像要使用此卷您使用以下命令运行 docker 容器:docker run --volumes-from some-volume docker-image-name:tag现在你有一个 docker 容器正在运行,它会将来自 some-volume 的卷安装在 /var/lib/mysqlYou have another docker image that you want to use this volumeYou run the docker container with the following:docker run --volumes-from some-volume docker-image-name:tagNow you have a docker container running that will have the volume from some-volume mounted in /var/lib/mysql注意:使用 --volumes-from 会将卷挂载到卷所在位置的任何内容上.即,如果您在 /var/lib/mysql 中有东西,它将被卷的内容替换.Note: Using --volumes-from will mount the volume over whatever exists in the location of the volume. I.e., if you had stuff in /var/lib/mysql, it will be replaced with the contents of the volume. 这篇关于docker 在主机上安装卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-13 10:27