My question is related to this question on copying files from containers to hosts; I have a Dockerfile that fetches dependencies, compiles a build artifact from source, and runs an executable. I also want to copy the build artifact (in my case it's a .zip
produced by sbt dist
in '../target/`, but I think this question also applies to jars, binaries, etc.
docker cp
works on containers, not images; do I need to start a container just to get a file out of it? In a script, I tried running /bin/bash
in interactive mode in the background, copying the file out, and then killing the container, but this seems kludgey. Is there a better way?
On the other hand, I would like to avoid unpacking a .tar
file after running docker save $IMAGENAME
just to get one file out (but that seems like the simplest, if slowest, option right now).
I would use docker volumes, e.g.:
docker run -v hostdir:out $IMAGENAME /bin/cp/../blah.zip /out
but I'm running boot2docker
in OSX and I don't know how to directly write to my mac host filesystem (read-write volumes are mounting inside my boot2docker VM, which means I can't easily share a script to extract blah.zip
from an image with others. Thoughts?
Unfortunately there doesn't seem to be a way to copy files directly from Docker images. You need to create a container first and then copy the file from the container.
However, if your image contains a
cat
command (and it will do in many cases), you can do it with a single command:If your image doesn't contain
cat
, simply create a container and use thedocker cp
command as suggested in Igor's answer.I am using boot2docker on MacOS. I can assure you that scripts based on "docker cp" are portable. Because any command is relayed inside boot2docker but then the binary stream is relayed back to the docker command line client running on your mac. So write operations from the docker client are executed inside the server and written back to the executing client instance!
I am sharing a backup script for docker volumes with any docker container I provide and my backup scripts are tested both on linux and MacOS with boot2docker. The backups can be easily exchanged between platforms. Basically I am executing the following command inside my script:
I have already moved archives between the linux container and my mac container without any adjustments to the backup or restore script. If this is what you want you find the whole script an tutorial here: blacklabelops/jenkins
Answering an old question for references. To copy a file from an image, create a temporary container, copy the file from it and then delete it:
Parent comment already showed how to use cat. You could also use tar in a similar fashion:
A much faster option is to copy the file from running container to a mounted volume:
VS