Dockerfile COPY from image to host

I do not know the details of the sepcific compiler tools you are using, but I think I get what you are trying to achieve:

I would not include the COPY command in the Dockerfile. The Dockerfile must only contain the necessary instructions to have an image with the necessary tools and dependencies to carry out the compilation process, and maybe a shell script with the specific compiling orders.

Now you run docker build and you have your image, let's call it mosq. Let's assume that:

  • You have your source code in your local machine in /home/me/my-source-code
  • Once complied, you have the result inside a subfolder dist of that folder: /home/me/my-source-code/dist/result.so
  • Your image has a script /compile.sh that compiles the source code present in /compilation (that folder should be empty in the image)

Then, you run the image mounting volume param: /home/me/my-source-code onto /compilation inside the container

Assuming all the previous points, the docker run command should look something similar to:

docker run -d --name my-compiler -v /home/me/my-source-code:/source mosq /compile.sh

Et voila, the container will run silently and die, and after that you'll have your compilation in /home/me/my-source-code/dist/result.so

The specifics might vary a lot depending on the details, but I hope you get the idea: prepare everything in your image so that executing a single sh script, the compiler takes the code from somewhere and runs. Mount a volume with the code in that folder. If the compiler outputs the result somewhere else, mount another volume from your host machine to get the result there.


COPY is probably not the right tool for what you are trying to achieve.

Either use a runtime volume, like @gmc suggests or copy it on the host using docker cp.

Usage

docker cp CONTAINER:SRC_PATH DEST_PATH

However, I'm not sure that is the right approach in general. It doesn't sound like Docker is the tool you need for what you are trying to achieve. If you want a mutable server instance, there are better options.