Home > other >  Copy file from docker to host before docker exits
Copy file from docker to host before docker exits

Time:11-19

I have a docker image created that is setup to build a file for me. The goal here is to just run the docker and pass it a command to build then I would get the final binary on my host somehow.

This is my run command:

docker run -it builder /bin/sh -c 'cd /go/project; go build main.go'

This will build my project and I will have a binary.

The issue is, once this completed the docker container will exit and the binary is gone. I have tried to run..

docker run -it builder /bin/sh -c 'cd /go/project; go build main.go' && docker cp builder:/go/project/main .

The only issue with this is the docker will have already closed at this point.

Is it possible to just redirect this file to host? Or able to keep the container open enough to copy then close it down?

I have tried doing my_addr()

CodePudding user response:

What you can do is simple create a shared volume between a directory and the path of your main.go file, and copy it there. So you will have your main.go file extracted at the end.

https://docs.docker.com/storage/volumes/

  • Related