Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

474
Views
How to retrieve file from docker container?

I have a simple Dockerfile which creates a zip file and I'm trying retrieve the zip file once it is ready. My Dockerfile looks like this:

FROM ubuntu

RUN apt-get update && apt-get install -y build-essentials gcc

ENTRYPOINT ["zip","-r","-9"]
CMD ["/lib64.zip", "/lib64"]

After reading through the docs I fee like something like this should do it but I can't quite get it to work.

docker build -t ubuntu-libs .
docker run -d --name ubuntu-libs --mount source=$(pwd)/,target=/lib64.zip ubuntu-libs

One other side question: Is is possible to rename the zip file from the command line?

Edit:

This is different than the duplicate question mentioned in the comments because while they're using cp to copy file from a running Docker container I'm trying to mount a directory upon instantiation.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

There are multiple ways to do this.

Using docker cp:

docker cp <container_hash>:/path/to/zip/file.zip /path/on/host/new_name.zip

Using docker volumes:

As you were leading to in your question, you can also mount a path from the container to your host. You can either do this by specifying where on the host you want the mount point to be or don't specify where the mount point is and let docker choose. Both these paths require different approaches.

Let docker choose host mount location

docker volume create random_volume_name
docker run -d --name ubuntu-libs -v random_volume_name:<path/to/mount/in/container> ubuntu-libs

The content will be located on your host, here:

ls -l /var/lib/docker/volumes/random_volume_name/_data/ 

Let me choose host mount location

docker run -d --name ubuntu-libs -v <existing/mount/point/on/host>:<path/to/mount/in/container> ubuntu-libs

This creates a clean/empty location that is shared as per the locations defined in the command. Now you need to modify your Dockerfile to copy the artifacts to this path, something like:

FROM ubuntu

RUN apt-get update && apt-get install -y build-essentials gcc

ENTRYPOINT ["zip","-r","-9"]
CMD ["sh", "-c", "/lib64.zip", "/lib64", "cp", "path/to/zip/file.zip", "<path/to/mount/in/container>"]

The content will now be located on your host, here:

ls -l <existing/mount/point/on/host>

I got to give a shout out to @joaofnfernandes from here, who does a great job explaining.

over 4 years ago · Santiago Trujillo Report

0

As @flagg19 commented, you should be binding a directory onto a directory. You can make up directories inside the container, and you can override the RUN arguments. Doing both plus adding type=bind leads to great success:

docker run -d --rm --mount type=bind,source="$(pwd)",target=/out ubuntu-libs /out/lib64.zip /lib64

Or of course you could change the Dockerfile RUN command to write to /out/lib64.zip instead of /lib64.zip:

FROM ubuntu

RUN apt-get update && apt-get install -y build-essentials gcc && mkdir /out

ENTRYPOINT ["zip","-r","-9"]
CMD ["/out/lib64.zip", "/lib64"]
docker run -d --rm --mount type=bind,source="$(pwd)",target=/out ubuntu-libs

Either way, I recommend adding --rm and getting rid of --name. No need to keep around the container after it's done.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!