I have built the image into a container, why I try to print the log of the container I run in detach mode I get an error printout :
Error: Could not find or load main class EazyBankApplication
Caused by: java.lang.ClassNotFoundException: EazyBankApplication
Please how can I fix this, I have tried changing the run command to accommodate a directory path and it still persists.
FROM openjdk:17
RUN mkdir /eazyApp
COPY ./src/main/java/com/eazybank/ /eazyApp
WORKDIR /eazyApp
CMD java EazyBankApplication
2
Answers
Your copy command does not copy the files in the directory, use
Also you don’t need mkdir command, copy command will create the directory.
In your Dockerfile, you’re
COPY
ing only a single source directory into the image. When the container tries to execute theCMD java EazyBankApplication
, the corresponding.class
file doesn’t exist.Many SO Java questions address this by compiling the application on the host, and then
COPY
the built jar file (but not any of its source) into the image.If you want to build the application inside Docker, then you need to invoke the build tool somehow. A typical approach is to
COPY
the entire application tree into the image, excluding parts of it via a.dockerignore
file.(You could combine the two approaches with a multi-stage build to get a final image without the source code, and possible with only the JRE for a smaller image.)
You probably need a build system like Gradle or Maven in practice, but if you only have the source files, you can still directly use
javac
.