简体   繁体   English

重新附加孤立的 docker 卷

[英]Reattaching orphaned docker volumes

I'm using a docker volume, specified in my dockerfile so that my data can persist on the host.我正在使用在我的 dockerfile 中指定的 docker 卷,以便我的数据可以保留在主机上。 The dockerfile looks something like this: dockerfile 看起来像这样:

FROM base-image
VOLUME /path/to/something
RUN do_stuff
....

When I run the container it creates a volume (call it VolumeA ) which I can see when I do a docker volume ls .当我运行容器时,它会创建一个卷(称为VolumeA ),当我执行docker docker volume ls时我可以看到它。

If I stop and remove the container, the VolumeA sticks around as expected.如果我停止并移除容器, VolumeA 会按预期固定。

My question is, if I run a new version of the container, is there a way to use VolumeA rather than have it create a new one?我的问题是,如果我运行一个新版本的容器,有没有办法使用VolumeA而不是让它创建一个新版本?

I prefer using named volumes, as you can mount them easily to a new container.我更喜欢使用命名卷,因为您可以轻松地将它们安装到新容器中。

But for unnamed volume, I:但是对于未命名的卷,我:

  • run my container (the VOLUME directive causes it to create a new volume to a new path that you can get by inspecting it)运行我的容器(VOLUME 指令会导致它创建一个新的卷到您可以通过检查它获得的新路径)
  • move the path of the old volume to that new path.将旧卷的路径移动到新路径。

Before docker volume commands , I used to do that with a script : updateDataContainerPath.sh .在使用docker volume 命令之前,我曾经使用脚本执行此操作updateDataContainerPath.sh

But again, these days, none of my images have a VOLUME in them: I create separately named volumes ( docker volume create ), and mount them to containers at runtime ( docker run -v my-named-volume:/my/path )但同样,这些天,我的图像都没有VOLUME :我创建单独命名的卷( docker volume create ),并在运行时将它们挂载到容器( docker run -v my-named-volume:/my/path

You might use the -v flag in the docker run command to bind the existing volume to your new docker container您可以在docker run命令中使用-v标志将现有卷绑定到新的 docker 容器

docker run -v VolumeA:/path/to/something [image]

Also look into the --volumes-from flag to mount volumes used or created by other containers.还要查看--volumes-from标志以挂载其他容器使用或创建的卷。

https://docs.docker.com/engine/reference/commandline/run/ https://docs.docker.com/engine/reference/commandline/volume_create/ https://docs.docker.com/engine/reference/commandline/run/ https://docs.docker.com/engine/reference/commandline/volume_create/

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

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