Docker Running In Cloud Run Jobs Throws ClassNotFoundException, Runs Fine On Docker Desktop

I’m building a java application to run on a schedule using Cloud Run Jobs. This application has a main method that is called to perform its task. I am packaging this up into a docker container that runs successfully on my local machine using Docker Desktop. I then tag and push the image to Artifact Registry. I then select the image when setting up a new Cloud Run Jobs Instance and it fails. It is failing and exiting with an error. The logs read as this:

2022-09-06T04:03:30.441244ZError: Could not find or load main class one.studio.Main Default 2022-09-06T04:03:30.441270ZCaused by: java.lang.ClassNotFoundException: one.studio.Main Warning 2022-09-06T04:03:30.449143ZContainer called exit(1).

This really confuses me about everything I’ve been learning about Docker, in that I thought the application is able to execute the same on each machine. I’m not really sure what to include for this, as I don’t see what would have an effect on this.

Dockerfile
FROM amazoncorretto:17

COPY target/classes/ /tmp

WORKDIR /tmp

ADD ./lib/mysql-socket-factory-1.5.0-jar-with-dependencies.jar lib/
ADD ./lib/mysql-connector-java-8.0.29.jar lib/
ADD ./lib/twilio-9.0.0-rc.3-jar-with-dependencies.jar lib/
ADD ./lib/my-erp-359002-2816e69f4a8a.json /tmp/lib/my-erp-359002-2816e69f4a8a.json

ENV CLASSPATH=lib/mysql-connector-java-8.0.29.jar:${CLASSPATH}
ENV CLASSPATH=lib/mysql-socket-factory-1.5.0-jar-with-dependencies.jar:${CLASSPATH}
ENV CLASSPATH=lib/twilio-9.0.0-rc.3-jar-with-dependencies.jar:${CLASSPATH}
ENV GOOGLE_APPLICATION_CREDENTIALS="/tmp/lib/my-erp-359002-2816e69f4a8a.json"

CMD java one.studio.Main
docker-compose.yml
version: "3"
services:
  web-app:
    build: .

My Project Structure

The only thing I see and I would not do is using /tmp for any persistent content. Your image should work the same way everywhere as long as it does not require something kernel-specific or you don’t run the container differently. /tmp is a tempory folder and if it used as a tmpfs, the content you copy to that folder will disappear. For example:

It may keep the content of tmp:

docker run --rm -it test bash

But the following command will hide it:

docker run --rm -it --tmpfs /tmp test bash

Try to copy files to an other folder.

This worked, thank you. Dockerfile has been updated as follows

FROM amazoncorretto:17

COPY target/classes/ /app

WORKDIR /app

ADD ./lib/mysql-socket-factory-1.5.0-jar-with-dependencies.jar lib/
ADD ./lib/mysql-connector-java-8.0.29.jar lib/
ADD ./lib/twilio-9.0.0-rc.3-jar-with-dependencies.jar lib/
ADD ./lib/my-erp-359002-2816e69f4a8a.json /lib/my-erp-359002-2816e69f4a8a.json

ENV CLASSPATH=lib/mysql-connector-java-8.0.29.jar:${CLASSPATH}
ENV CLASSPATH=lib/mysql-socket-factory-1.5.0-jar-with-dependencies.jar:${CLASSPATH}
ENV CLASSPATH=lib/twilio-9.0.0-rc.3-jar-with-dependencies.jar:${CLASSPATH}
ENV GOOGLE_APPLICATION_CREDENTIALS="/lib/my-erp-359002-2816e69f4a8a.json"

CMD ["java", "one.studio.Main"]