Is there a way to copy a file from an image to the local filesystem without running a container?

Our team has been building docker images that contain metadata manifest files. Ideally, before we run an image, we’d love to inspect that metadata file, using something like:

docker cp image-name:/path/to/manifest /local/path/to/manifest

so that we can make decisions about how we want to run it. Of course it’s possible to get at our files by creating a throwaway container:

docker run --name temp-container-name image-name /bin/true
docker cp temp-container-name:/path/to/manifest /local/path/to/manifest
docker rm temp-container-name

But is there a way to copy a file directly from an image without first creating a container?

2 Likes

Does anyone have an answer?

don’t know… but you can drop one step, the docker rm , if you do the docker run with --rm as a parm

and https://blog.ropnop.com/plundering-docker-images/
and http://blog.oddbit.com/2015/02/13/unpacking-docker-images/

I’m only reading articles at the moment, but the OCI toolset and/or similar tools (looking at umoci atm) may do what you want.

I’ve only spent ~ 5 minutes looking at this and it looks like there are methods to ‘unpack’ an image. Wether or not they allow you to pull a single file though … idk.

Please let me know how it goes if you try them. It will take me a while before I’ll be able to compile/run/test the tools myself. (I have a similar use-case)

Use docker create to create the throwaway container without running it. then run your docker cp. Still creates a container though.

2 Likes

Here is an example. In my case, I want to copy a file which is located at /usr/src/app/build/pgbackrest-release-2.43/src/pgbackrest inside the image hm-pgbackrest:latest.

First method (based on @wh00 's post)

docker create --name=hm-pgbackrest hm-pgbackrest:latest
docker cp hm-pgbackrest:/usr/src/app/build/pgbackrest-release-2.43/src/pgbackrest pgbackrest

Second method

docker export "$(docker create hm-pgbackrest:latest)" > /tmp/image.tar
tar -x -f /tmp/image.tar -C /tmp/ usr/src/app/build/pgbackrest-release-2.43/src/pgbackrest
cp /tmp/usr/src/app/build/pgbackrest-release-2.43/src/pgbackrest pgbackrest