Specying it as part of your run command as docker run -v /some/dir
Either way, these two things do exactly the same thing. It tells Docker to create a directory on the host, within the docker root path (by default /var/lib/docker),
With a "normal" volume, docker will automatically copy data at the specified volume path (e.g. /some/path, above) into the new directory that was created by docker, with a "bind-mount" volume this does not happen.
When you docker rm -v my_container a container with "bind-mount" volumes, the "bind-mount" volumes will not be removed.
Bind-mount volume所属容器都被删除后,其对应的host目录不会被删除。
Normal volume 在其被引用的容器都被删除会自动删除该目录
volume指定的目录如果不存在,容器在启动(在docker run -v中定义)或者镜像生成的时候(定义在Dockfile中)会自动创建该目录,而且需要注意的是,一旦host的目录挂载到volume指定的路径上,已经在volume中创建的数据会不见(之前在Dockfile中创建的文件在mount后会不见,不过一旦你取消挂载你就可以看见该文件,它并没有被删除),你所看到的都是host主机目录中的文件,而之后容器在volume中的任何操作都将持久化在host主机目录中。
在一些镜像dockfile中看到
volume /root/.m2
这个配置,我明白volume表示数据卷,可供外部系统挂载。但我不清楚的是这个配置文件并没有指定host的路径,在我们运行容器的时候还是需要在命令中指明host的路径如/home:/root/.m2
,那如此声明有什么意义?有人说是会自动在host选取默认路径绑定在该挂载点上,但是在mac上kitematic中并没有挂载路径。 那如此声明有什么意义?查阅了一些资料发现,在linux环境下其实会在docker容器运行的路径下生成一个目录直接挂载在Dockfile文件中volume属性指定的路径上。
docker-indepth-volumes
一文中提及:我们把这种方式指定的volume称为
normal volume
,而在运行时通过-v参数指定挂载在volume上的host目录的方式称为Bind-mount volume
.这二者有如下区别volume指定的目录如果不存在,容器在启动(在
docker run -v
中定义)或者镜像生成的时候(定义在Dockfile中)会自动创建该目录,而且需要注意的是,一旦host的目录挂载到volume指定的路径上,已经在volume中创建的数据会不见(之前在Dockfile中创建的文件在mount后会不见,不过一旦你取消挂载你就可以看见该文件,它并没有被删除),你所看到的都是host主机目录中的文件,而之后容器在volume中的任何操作都将持久化在host主机目录中。