Typically, tutorials teach you how to write a Dockerfile to containerize a Java application. A Dockerfile can be error prone and it's hard to implement all the best practices. Rather than writing a Dockerfile, use tools such as Jib and Buildpacks to automatically create optimized container images.
Build and Push
Most tools can build and push directly into a container registry. In case of Jib, this step does not require a Docker daemon at all, and it can push changed layers directly into a remote registry. This is great for automated CI/CD pipelines.
Jib can containerize any Java application easily, without a Dockerfile nor docker installed. Jib will push the container image directly to the remote registry.
Paketo Buildpack will calculate the minimum memory needed to run the Spring Boot application. For this Hello World example, the minimum is 1GB of RAM.
Build Locally
If you are running a local Docker daemon and you do not want to push straight to a remote registry, then you can build container images without pushing:
PROJECT_ID=$(gcloudconfigget-valueproject)# Maven with Paketo Buildpack./mvnwspring-boot:build-image \-Dspring-boot.build-image.imageName=gcr.io/${PROJECT_ID}/helloworld# Maven with GCP Buildpack./mvnwspring-boot:build-image \-Dspring-boot.build-image.imageName=gcr.io/${PROJECT_ID}/helloworld \-Dspring-boot.build-image.builder=gcr.io/buildpacks/builder# Gradle with Paketo Buildpack./gradlewbootBuildImage--imageName=gcr.io/${PROJECT_ID}/helloworld# Gradle with GCP Buildpack./gradlewbootBuildImage--imageName=gcr.io/${PROJECT_ID}/helloworld \--builder=gcr.io/buildpacks/builder
Run Locally
If you have Docker installed locally, you can run the docker container locally to ensure everything works. This command will run the container locally and forward localhost's port 8080 to the container instance's port 8080.