Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile COPY from image to host

I have a Dockerfile in which I first compile the Mosquitto server in one layer, then use COPY to copy the source files of an authentication plug-in into the image and finally RUN the compilation of that plug-in.

All in all, the resulting image is good to be used in a container which then has the Mosquitto server running with that plug-in loaded.

I want to modify this plug-in and recompile it by re-running the build of the Dockerfile. Since the first layer is unmodified, it just copies the modified files and runs the compilation again.

What I want to do now is to extract the plug-in (.so file) from that new image and move it to a mounted directory on the host, so that the currently running Mosquitto server would only need to be restarted.

Is it possible to use the COPY command in reverse, so that it copies the compiled plug-in to a specified host directory so that I can then delete the newly created image?

Or is this a bad approach altogether? Should I better exec into the running container and have it rebuild the plug-in (which would limit me to building the plug-in on the machine on which the server is running)?

like image 853
Daniel F Avatar asked Feb 02 '19 22:02

Daniel F


People also ask

How do I copy a directory from container to host?

To summarize, follow these steps to copy a file from a Docker container to a host machine: Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container.

Can we copy Docker images from one host to another?

Copy Docker images from one host to another by tar files Docker allows you to save images into tar files using the docker save . This command also compresses images and enables sharing them easily and quickly. You can then use the docker load command to load the Docker image back from the tar file into another host.

Can we extract files from Docker image?

– export the image, extract it and the archives inside it and get your file: docker save repo:tag > image. tar then tar xf image. tar to extract files of the tar. Repeat the tar step for each tar in the subfolders.


2 Answers

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.

like image 136
gmc Avatar answered Sep 25 '22 00:09

gmc


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.

like image 34
Ryan Avatar answered Sep 23 '22 00:09

Ryan