Copy a file from a Docker to another Docker and execute it

404 Views Asked by At

As I mentioned above, I want to copy an executable file from a docker to another docker and run it.

docker cp target/demo.jar test:/demo.jar

is not working since docker cp can copy only "docker to host" or "host to docker".

as a next step, I need to run the executable from the source docker itself. Can somebody help me with this?

1

There are 1 best solutions below

0
On

Long story short: You don't do that. Copying a file at runtime to the container itself is possible but not recommended. This will alter the container state meaning that it cannot be easily replaced with a new instance as they will differ.

As per comments I could see you use the openjdk image. This image is just a base image so it has no running process. Therefor it will immediately stop after trying to run it bare. This means you will have to create a Dockerfile based on that container and extend it to run your process.

If you have a fixed file that is not prone to changes you could copy it in the same directory as your Dockerfile and use the COPY or ADD directive to make it available in the image. This would imply that you need to recreate the image everytime a change is made to the JAR file.

On the other hand, if you have a changing file and you don't want to keep recreating the image, try going for a mount situation. This has it's own disadvantages but it would allow every new instance of your container work with the latest version of said file.

Do note: When you create the image and run the container it might quit after execution. This is due to the nature of docker containers, where the last (or main) process can either be infinite (like a webserver) or finite (like a script).