在Docker中将数据添加到现有命名卷的正确方法是什么? [英] What is the right way to add data to an existing named volume in Docker?
问题描述
我以旧的方式使用Docker并使用了一个卷容器:
I was using Docker in the old way, with a volume container:
docker run -d --name jenkins-data jenkins:tag echo "data-only container for Jenkins"
但是现在我改成了新方法通过创建一个命名卷:
But now I changed to the new way by creating a named volume:
docker volume create --name my-jenkins-volume
我将此新卷绑定到新的Jenkins容器。
我唯一剩下的是一个文件夹,其中有我以前的jenkins容器的 / var / jenkins_home
。 (通过使用 docker cp
)
现在,我要用该文件夹的内容填充新的命名卷。
I bound this new volume to a new Jenkins container.
The only thing I've left is a folder in which I have the /var/jenkins_home
of my previous jenkins container. (by using docker cp
)
Now I want to fill my new named volume with the content of that folder.
我可以仅将该文件夹的内容复制到 / var / lib / jenkins / volume / my-jenkins-volume / _data
吗?
Can I just copy the content of that folder to /var/lib/jenkins/volume/my-jenkins-volume/_data
?
推荐答案
您可以当然可以将数据直接复制到 / var / lib / docker / volumes / my- jenkins-volume / _data
,但是通过这样做,您可以:
You can certainly copy data directly into /var/lib/docker/volumes/my-jenkins-volume/_data
, but by doing this you are:
-
依靠物理访问Docker主机。如果您正在与远程docker api交互,则此技术将不起作用。
Relying on physical access to the docker host. This technique won't work if you're interacting with a remote docker api.
依靠卷实现的特定方面将来可能会发生变化,打破了您依赖它的任何流程。
Relying on a particular aspect of the volume implementation would could change in the future, breaking any processes you have that rely on it.
我认为您最好依靠可以完成的事情通过命令行客户端使用docker api。最简单的解决方案可能只是使用一个辅助容器,例如:
I think you are better off relying on things you can accomplish using the docker api, via the command line client. The easiest solution is probably just to use a helper container, something like:
docker run -v my-jenkins-volume:/data --name helper busybox true
docker cp . helper:/data
docker rm helper
这篇关于在Docker中将数据添加到现有命名卷的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!