Docker – How to Extract a File from a Docker Image

docker

I'ld like to extract a file from a Docker image without having to run the image.

The docker save option is not currrently a viable option for me as it's saving too huge of a file just to un-tar a specific file.

Best Answer

You can extract files from an image with the following commands:

docker create $image  # returns container ID
docker cp $container_id:$source_path $destination_path
docker rm $container_id

According to the docker create documentation, this doesn't run the container:

The docker create command creates a writeable container layer over the specified image and prepares it for running the specified command. The container ID is then printed to STDOUT. This is similar to docker run -d except the container is never started. You can then use the docker start <container_id> command to start the container at any point.


For reference (my previous answer), a less efficient way of extracting a file from an image is the following:

docker run some_image cat $file_path > $output_path
Related Question