-3

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?

RDD
  • 145
  • 1
  • 18
  • Do you want to copy the jar file from an _image_ to another image, and then run a new container from that other image? The Dockerfile [`COPY --from=...`](https://docs.docker.com/engine/reference/builder/#copy) option can target an arbitrary image, it's not limited to multi-stage builds. – David Maze Oct 05 '21 at 10:34
  • the new container is already running, I want to copy the jar and execute it. And it's in the deployment stage of my yml file. – RDD Oct 05 '21 at 10:40
  • If you want to copy from (running) container to (running) container it would try to avoid that situation and instead use the same mount on both instances so they can read/write/share files at runtime. – JustLudo Oct 05 '21 at 10:41
  • If the new container is already running, and you want it to run this jar file, then how did it get there? (What's the `docker run` command for the target container?) If you need to run multiple applications, can you run them in multiple separate containers? – David Maze Oct 05 '21 at 10:42
  • It's bad practice to store and copy files to running containers as this manipulates their state, which can easily be made undone by rebooting the container (or replacing it with a new instance). – JustLudo Oct 05 '21 at 10:42
  • 1
    (A container typically only runs a single process, and it's intended to be light-weight and somewhat disposable. If you replaced "container" with "JVM" in the question, what would your workflow look like?) – David Maze Oct 05 '21 at 10:44
  • I accept my misunderstanding about the running an executable jar inside a running container. which can be removed, so I can copy the file and run it. destination docker is an openjdk11 image. – RDD Oct 05 '21 at 10:47
  • 1
    Do note; even though a container is intended to have infinite running time (i.e. an endless loop) it can be used to run a single process and then quit afterwards. So in theory it would be possible to start a minimal java container and have the last process be "run this JAR file". Replace the JAR file with the location of a shared mount and filepath. – JustLudo Oct 05 '21 at 10:50
  • currently, I'm running destination docker with `docker run --name test -d -p 8061:8080 openjdk` – RDD Oct 05 '21 at 10:52
  • 1
    That won't do much. There is no main process in that image. So make your own dockerfile based on this image, mount a file and RUN the main process as it's last step. You should be good to go then. Alternatively if you have a file before runtime you can copy it into the image and run it from there. You will need to recreate the image every time an update to the jar file occurs though. – JustLudo Oct 05 '21 at 10:58

1 Answers1

1

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).

JustLudo
  • 1,690
  • 12
  • 29