-1

I'm new to Docker. I have a problem: I have a jar file that processes "in.png" and saves the result as a separate file: out.png. I'd like to create a docker image and put a .jar file in it. It is important that the in.png and out.png files appear / are delivered on the host side. I want to put everything on dockerhub, and ultimately allow the user to process their own graphic files. What is the best solution to this problem? I have tryed something like this (is it good solution?):

FROM java:8
WORKDIR /
ADD Hello.jar in.png
EXPOSE 8080
CMD java - jar Hello.jar

But i can't coppy (or i don't know how) output file from container to host;

Or maybe a better solution is make an image (Java / Ubuntu with Java?), uploading a .jar file to it and providing the user with a set of commands, e.g .:

docker cp Hello.jar 4e673836297e:/
docker cp in.png 4e673836297e:/
docker run ubuntu java -jar Hello.jar
docker cp 4e673836297e:/ . 

Please tell me what the best solution is for this problem

  • Instead of running a JVM in Docker, why not directly run the JVM on the host? That will be able to directly access host files without special setup. – David Maze Feb 10 '21 at 18:43

1 Answers1

0

You should mount a directory to your docker container using bind mounts.

If your jar writes the output to /output/out.png you can start a container based on your image with the following command.

docker run -v "$(pwd)":/output YOUR_IMAGE_NAME

That option mounts the current working directory to the /outpout folder inside the container. So the files written there will be visible on the outside.

A more detailed explanation can be found here: https://docs.docker.com/storage/bind-mounts/


Edit to answer the additional question:

You can also use the mounted folder for the input, so you can have different image files as input.

First the simple aproach. Instead of naming the folder output, you could name it workdir and mount the current directory to it.

docker run -v "$(pwd)":/workdir YOUR_IMAGE_NAME

When you have to change your java application to read the in.png from /workdir/in.png. The user has to save his individual input file as in.png in the current folder and can then run the Docker image.

A more advanced and more comfortable aproach is the following. You change the Dockerfile so it contains an entrypoint and a command.

FROM java:8
WORKDIR /
ADD Hello.jar in.png
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "Hello.jar"]
CMD ["in.png"]

When this docker image is executed, it starts java -jar Hello.jar but with the parameters from the CMD added. The CMD can be overwritten when the image is executed:

docker run -v "$(pwd)":/workdir YOUR_IMAGE_NAME custom.png

You have to change your programm, so that it accepts the first command line parameter as the name of the input file inside the workdir.

Then the user can tell your progamm the name of his or her image file.

seism0saurus
  • 396
  • 2
  • 10