Docker cannot find the jar file

I’m creating an docker image file for the jar file that I have to further use it in the Kubernetes cluster.

The image builds up but when I run this image as container, it fails with error
Error: Unable to access jarfile <–filename>

If I run the same image with -it option it runs as a container and the file is present in the filesystem of the container, but I doubt it this really a solution

docker run --name adaptercont1 -it adapterjar:1.0 /bin/bash

Dockerfile

FROM openjdk:25-jdk-slim

RUN mkdir /usr/src/adapterpod

WORKDIR /adapterpod

COPY myjarfile-24.01.00.003.jar /usr/src/adapterpod/

CMD ["java", "-jar", "/usr/src/adapterpod/myjarfile-24.01.00.003.jar -m localhost -Port 3000 -Key myKey -podTenant Java -podName MyPOD"]

This seems strange. Workdir is inside container.

Why would you expect this to work?

A short look in the docs should have made clear why it fails: https://docs.docker.com/reference/dockerfile/#cmd

You are using the exec form wrong. If you had shared the actual error message, it would have been obvious right away, because what you replace with <-filename> actually was /usr/src/adapterpod/myjarfile-24.01.00.003.jar -m localhost -Port 3000 -Key myKey -podTenant Java -podName MyPOD. Of course, Java is not able to find a jar with that name. You need to split each parameter into its own string, like shown in the docs.

1 Like

The jar executes in command line when run separately but fails in the container.

added here for COPY command

I’m not sure what you mean by “for COPY command”. Please, write more detailed replies so we understand what you are referring to. @bluepuma77 is right. You have a workdir but you don’t copy anything there. It could be intentional, but it is indeed strange.

@meyay was also right. Your CMD was incorrect. I’m pretty sure you don’t use that same line in the terminal, as CMD is for the Dockerfile only. And you haven’t shared the real error message yet to explain why you think what you used should work. But it shouldn’t. Unless the “java” that you call is not the java we know, but just something with the same name.

1 Like

As @meyay points out, your CMD line is wrong. The format in which you have provided the arguments expects every element of the command line to be in a separate pair of quotes. So, the CMD line should have been written like this:

CMD ["java", "-jar", "/usr/src/adapterpod/myjarfile-24.01.00.003.jar", "-m", "localhost" "-Port", "3000", "-Key", "myKey",  "-podTenant", "Java", "-podName", "MyPOD"]

This has to do with how command-line arguments are passed to the created process. When you type this command in the command line, the separation is done by the shell. When you write a CMD or an ENTRYPOINT in a Dockerfile, you have to do the separation yourself, as shown.

Also, I have the same question as @bluepuma77: why the WORKDIR ? The Dockerfile as written will create a directory called /usr/src/adapterpod, another directory called /adapterpod, and copy the jar file into /usr/src/adapterpod. When a container is created and started, the java command will execute with the (empty) /adapterpod as the current directory. Is this what you want?

Thanks Raj, this worked.

I have to change some parameter values for port and hostname.

Also, changed jdk to zulu (compatibility issue)

my next question, what will be the best way to make this jar available, for an application using/referencing this jar file on Kubernetes cluster ?

Containers are isolated processes, which means that their component parts cannot even be seen by other processes or applications, let alone shared. So no application other than the one packaged in this image will be able to use the jar file in that image.

You should re-evaluate if you really need containers for whatver it is you are ultimately trying to do. If all you want is to create reusable components for applications, you don’t need containers.

We have a custom application build on Docker and the team want to migrate it on the Kubernetes platform. I was providing this jar as configmap but it exceeds the size, thus created an image of it to run as a separate container and the main mcu can reference this jar

No, that won’t work. The contents of an image cannot directly be injected into the filesystem of a container created from another image.

Try this:

  1. Build an image which, when run, simply copies the jar to a path and exits.
  2. Use that image as as InitContainer in whichever pods need the jar. Mount a volume to the path to which the jar is copied.
  3. Now, mount the same volume in the container(s) defined in your main application podspecs, and ensure that the main application picks up the jar from there.

Please, don’t create personal topics to ask all your questions. One topic is for one question that someone can solve and you can click on the Solution icon (rectangle with a check mark character in it) so visitors can find the solution quickly. Since you asked your new question in the same post where you thanked for the answer to the original question, I could not move new comments to a new topic to reply, so in short: what you described could be done by creating an image from scratch, copying the jar and someone else can build their own image copying the jar from yours. But containers are as @rajchaudhuri already wrote.

If you have further questions about the second issue, please, create a new topic.

I closed this topic and I’m going to mark @rajchaudhuri’s answer as solution. You will see the “solution” label as an example.