docker 在主机上安装卷 [英] docker mounting volumes on host

查看:31
本文介绍了docker 在主机上安装卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地能够使用

docker run -v /host/path:/container/path ...

但我的问题是这与在 Dockerfile 中使用 VOLUME 命令有什么区别

But my question is what the difference between this and using the VOLUME command in the Dockerfile

VOLUME /path

我正在使用带有 VOLUME 命令的图像,我想知道如何与我的主机共享它.我已经使用上面的 -v 命令完成了它,但我不知道我是否需要 -vVOLUME.

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.

示例:

  1. 您有一个 Dockerfile,它定义了一个 VOLUME/var/lib/mysql.
  2. 您构建 docker 镜像并标记它some-volume
  3. 你运行容器

然后,

  1. 您有另一个 docker 镜像要使用此卷
  2. 您使用以下命令运行 docker 容器:docker run --volumes-from some-volume docker-image-name:tag
  3. 现在你有一个 docker 容器正在运行,它会将来自 some-volume 的卷安装在 /var/lib/mysql
  1. You have another docker image that you want to use this volume
  2. You run the docker container with the following: docker run --volumes-from some-volume docker-image-name:tag
  3. Now 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 在主机上安装卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆