Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to copy a file out of a docker image without actually running the container from that image

Tags:

docker

I want to copy some file from a docker image without actually running a container from that image. Is it possible to do so? If yes, what would be steps required ?

like image 250
Umair Aslam Avatar asked Jan 15 '18 15:01

Umair Aslam


2 Answers

There's not a docker cp for images because images are inmutable objects but:

you can create a non running container:

docker create --name cont1 some-image
docker cp cont1:/some/dir/file.tmp file.tmp
docker rm cont1

The full not accepted (now rejected) proposal for docker cp on images is here:

https://github.com/moby/moby/issues/16079

like image 83
Alfonso Tienda Avatar answered Oct 13 '22 03:10

Alfonso Tienda


Here's a one-liner to copy a file from a Docker Image, through a container:

Image defines CMD

docker run --rm \
           -v $(pwd):/binary my-image /bin/sh -c "cp /kube-bench /binary"

Image defines ENTRYPOINT

docker run --rm --entrypoint "/bin/sh" \
           -v $(pwd):/binary kube-bench-image -c "cp /kube-bench /binary"

Both commands mounts the current directory in order to copy the file /kube-bench to it.

like image 3
Marcello de Sales Avatar answered Oct 13 '22 03:10

Marcello de Sales