Pushing a Scala 3 JAR/Docker file to Google Cloud Run

As a quick blog post, these are some notes and steps I used to create a Scala JAR file, put that in a Docker image, and then push it up to Google Cloud Run. Rather than add a lot of description here, I’ll just share my notes in a numbered order.

Note that I have cross-posted this article on my Valley Programming website at Pushing a Scala JAR/Docker file to Google Cloud Run to create a service.

1.Create my Scala web app and JAR file

As the first step, I use an old Scala webserver I wrote, and then built a JAR file from it using sbt and sbt-assembly. The resulting JAR file is below the current project directory, and has this name/path:

target/scala-3.1.0/WebServerDocker3-assembly-0.1.0.jar WebServerDocker3-assembly-0.1.0.jar

2. Create a Dockerfile for it

Next, create a Dockerfile that describes what I want in my Docker image:

FROM openjdk:11
WORKDIR /home
COPY target/scala-3.1.0/WebServerDocker3-assembly-0.1.0.jar WebServerDocker3-assembly-0.1.0.jar
EXPOSE 8080
CMD ["java", "-jar", "WebServerDocker3-assembly-0.1.0.jar"]
#CMD java -jar WebServerDocker3-assembly-0.1.0.jar

I kept that last comment in there because it shows another way to run the JAR file.

3. Build my Docker image

Build my image with this new command (adding in --platform):

$ docker build --platform linux/amd64 -t scala-webserver-1 .

4. Make sure I’m authenticated to Google Cloud

Make sure I’m authenticated to the Google Cloud. I don’t remember if this is the correct command, and note that it also depends on the location of the servers you set up through the Google Cloud web interface:

$ gcloud auth configure-docker us-central1-docker.pkg.dev

5. Determine/calculate what the Docker image needs to be

My Docker image name starts like this:

scala-webserver-1

Then you have to name it per the Google Cloud specs to be like this:

us-central1-docker.pkg.dev/cloud-run-project-name/my-repo-name/scala-webserver-1

Note that in January, 2022, I’m using the Google Cloud Artifact Registry. (Note: Container Registry is OLD, Artifact Registry is NEW.)

6. Tag the local Docker image with the GCP repository name

Next, you also have to tag the Docker image with the correct Google Cloud name:

# EXAMPLE:
$ docker tag SOURCE-IMAGE LOCATION-docker.pkg.dev/PROJECT-ID/REPOSITORY/IMAGE

# MINE:
$ docker tag scala-webserver-1 us-central1-docker.pkg.dev/cloud-run-1st-java-project/first-jar-repo/scala-webserver-1

Now you should see something like this:

# WORKED (there is no output from that command, but it works, as shown here)
$ docker images
REPOSITORY                                                                         TAG       IMAGE ID       CREATED         SIZE
scala-webserver-1                                                                  latest    ef18f546c1ca   6 minutes ago   667MB
us-central1-docker.pkg.dev/cloud-run-project-name/my-repo-name/scala-webserver-1   latest    ef18f546c1ca   6 minutes ago   667MB

7. Do the push to Google Cloud Run

After following those steps you can do the push to the Google Cloud:

$ docker push us-central1-docker.pkg.dev/cloud-run-project-name/my-repo-name/scala-webserver-1
Using default tag: latest
The push refers to repository [us-central1-docker.pkg.dev/cloud-run-1st-java-project/first-jar-repo/scala-webserver-1]
7720f7deb6fc: Pushed 
more output here ...
more output here ...
11936051f93b: Pushed 
latest: digest: sha256:854adc82b72ac3fe72da8a... size: 2212

8. Go back to the Google Cloud Run GUI and re-create a service

In the Google Cloud Run web app. The two main parts are:

  • Container image URL
    • Use the "SELECT" button to find the new Docker image in the Artifact Repository
  • Service name
    • scala-webserver-1
  • Choose to run the web app on one server, 10, 100, 1,000, etc.

If all goes well, after you submit the form you’ll be given a custom URL where your web-app is running. Then you can go to that URL, test your app, and also look at the logs.

Summary

These are just my notes — this isn’t a tutorial — but if you ever need to run a Scala JAR file on Google Cloud Run, I hope these notes are helpful.

Helpful URLs:

  • https://cloud.google.com/run/docs/deploying (deploying container images)
  • https://cloud.google.com/run/docs/quickstarts/build-and-deploy/other
  • https://cloud.google.com/artifact-registry/docs/docker/pushing-and-pulling
  • https://cloud.google.com/run/docs/quickstarts
  • https://cloud.google.com/compute/docs/quickstart-linux
  • https://cloud.google.com/gcp/getting-started
  • https://cloud.google.com/artifact-registry/docs/docker/quickstart