2

I have a container that generates an mp4 file for me, which I want to download / copy to my host system after the container script has been completed.

What I managed to do is add a sleep 2m to the end of my entrypoint.sh before calling exit 0 (it's a bit hacky, I know 😅) which gives me 2 minutes to run the following command to download the mp4 file.

docker cp gource_container:/output/generated.mp4 ./

This works fine when i do it by hand, but my goal is to run it automatically via bash script.

Is there any way to cp the file to my host system after the container exits?


When I run the cp command after the container is done without sleep i get the following error:

$ docker cp gource_container:/output/generated.mp4 ./
Error: No such container:path: gource_container:/output/generated.mp4
1
  • 1
    Well. Why you don't mount your container volume to the host machine instead copy it? The file still exists in the host machine after the container is exit. Commented Aug 26, 2020 at 15:22

1 Answer 1

4

A possible solution would be to mount your local working directory when creating a container from the image, and then copy the file over:

Example Dockerfile:

FROM ubuntu
RUN echo 'foo' > /foo.txt

Example commands:

# Build a Docker image and tag it test
$ docker build -t test .

# Launch a Docker container from the test image,mount the working directory to the /data directory on the container then copy the file of interest
$ docker run --rm -it -v $(pwd):/data test cp /foo.txt /data

# Inspect file locally
$ cat ./foo.txt
foo
Sign up to request clarification or add additional context in comments.

2 Comments

What does this line do? -v $(pwd):/data
It mounts your working directory (pwd) to a volume named data in the container

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.