What is the right way to add data to an existing named volume in Docker?

后端 未结 4 800
感情败类
感情败类 2020-11-29 16:43

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\"

相关标签:
4条回答
  • 2020-11-29 17:21

    You can certainly copy data directly into /var/lib/docker/volumes/my-jenkins-volume/_data, but by doing this you are:

    • 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.

    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
    
    0 讨论(0)
  • 2020-11-29 17:26

    Here are steps for copying contents of ~/data to docker volume named my-vol

    Step 1. Attach the volume to a "temporary" container. For that run in terminal this command :

    docker run --rm -it --name alpine --mount type=volume,source=my-vol,target=/data alpine

    Step 2. Copy contents of ~/data into my-vol . For that run this commands in new terminal window :

    cd ~/data docker cp . alpine:/data

    This will copy contents of ~/data into my-vol volume. After copy exit the temporary container.

    0 讨论(0)
  • 2020-11-29 17:28

    You can reduce the accepted answer to one line using, e.g.

    docker run --rm -v `pwd`:/src -v my-jenkins-volume:/data busybox cp -r /src /data
    
    0 讨论(0)
  • 2020-11-29 17:28

    You don't need to start some container to add data to already existing named volume, just create a container and copy data there:

    docker container create --name temp -v my-jenkins-volume:/data busybox
    docker cp . temp:/data
    docker rm temp
    
    0 讨论(0)
提交回复
热议问题