Building Java containers with Jib

Jib builds containers without using a Dockerfile or requiring a Docker installation. You can use Jib in the Jib plugins for Maven or Gradle, or you can use the Jib Java library.

What does Jib do?

Jib handles all steps of packaging your application into a container image. You don't need to know best practices for creating Dockerfiles or have Docker installed.

Docker build flow:

Diagram showing the stages from
       project to container registry using Docker.

Jib build flow:

Diagram showing no intermediary
       stages from project to container registry using Jib.

Jib organizes your application into distinct layers; dependencies, resources, and classes; and utilizes Docker image layer caching to keep builds fast by only rebuilding changes. Jib's layer organization and small base image keeps overall image size small which improves performance and portability.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the Cloud Console, on the project selector page, select or create a Cloud project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Google Cloud project. Learn how to confirm billing is enabled for your project.

  4. Enable the Container Registry API.

    Enable the API

  5. If you don't have Java, download, install, and configure it.
  6. Install Maven 3.5 or newer.
  7. Install and initialize the Cloud SDK.
  8. Authenticate to Container Registry, using `gcloud` as a Docker credential helper:
    gcloud auth configure-docker
    Optionally, refer to Jib configuration documentation for other ways of authenticating.

Building using Jib

  1. Select an existing project and navigate to the source folder or clone the sample with the following command:

         git clone
         cd java-docs-samples/run/helloworld

  2. Add the plugin to your pom.xml:



    • PROJECT with your Google Cloud project ID.
    • IMAGE with your image name.

    If you don't want to modify your pom.xml, you can use the command:

        mvn compile \
  3. Build and push the image to a container registry:

    mvn compile jib:build

    You can verify success by viewing your container in your Google Container Registry.

    Optionally, if you have Docker installed, you can build to your local Docker installation, so you can inspect or run the image as any other local container:

    mvn compile jib:dockerBuild

Additional customizations

You can customize your Jib builds in similar ways to Dockerfiles, such as adding environment variables and selecting a base image.

Add environment variables

You can add environment variables to your build similar to the ENV instruction in a Dockerfile, as follows:



  • PROJECT with your Google Cloud project ID.
  • IMAGE with your image name.
  • ENV_VAR with NAME.
  • VALUE with the desired value.

Now your application will respond with Hello <var>VALUE</var>!

Change the base image

The base image is equivalent to the FROM instructions in a Dockerfile. By default, Jib uses distroless/java as the base image. Distroless images contain only runtime dependencies. They do not contain package managers, shells or any other programs you would expect to find in a standard Linux distribution. The base image can be updated by changing the field from.image.

If you would like to include a shell for debugging, set the base image to alpine:3 or openjdk:VERSION (if Java is needed).

In order to add system packages, you will have to create a base image with those packages installed. Refer to Building containers for more information.


Try updating the from.image field to another Java base image, such as openjdk:8-alpine.

Customizing other Java aspects

Jib supports Java runtime configurations that may be needed to run your application. For more customizations, see Extended Usage.

Shipping the code

Setting up gcloud defaults

To configure gcloud with defaults for your Cloud Run service:

  1. Set your default project:

    gcloud config set project PROJECT-ID

    Replace PROJECT-ID with the name of the project you created for this tutorial.

  2. If you are using Cloud Run (fully managed), configure gcloud for your chosen region:

    gcloud config set run/region REGION

    Replace REGION with the supported Cloud Run region of your choice.

  3. If you are using Cloud Run for Anthos on Google Cloud, configure gcloud for your cluster:

    gcloud config set run/cluster CLUSTER-NAME
    gcloud config set run/cluster_location REGION


    • CLUSTER-NAME with the name you used for your cluster,
    • REGION with the supported cluster location of your choice.

Cloud Run locations

Cloud Run is regional, which means the infrastructure that runs your Cloud Run services is located in a specific region and is managed by Google to be redundantly available across all the zones within that region.

Meeting your latency, availability, or durability requirements are primary factors for selecting the region where your Cloud Run services are run. You can generally select the region nearest to your users but you should consider the location of the other Google Cloud products that are used by your Cloud Run service. Using Google Cloud products together across multiple locations can affect your service's latency as well as cost.

Cloud Run is available in the following regions:

  • asia-east1 (Taiwan)
  • asia-northeast1 (Tokyo)
  • europe-north1 (Finland)
  • europe-west1 (Belgium)
  • europe-west4 (Netherlands)
  • us-central1 (Iowa)
  • us-east1 (South Carolina)
  • us-east4 (Northern Virginia)
  • us-west1 (Oregon)

If you already created a Cloud Run service, you can view the region in the Cloud Run dashboard in the Cloud Console.

Deploy to Cloud Run

Deploy your container from Container Registry using the Cloud SDK to Cloud Run or Cloud Run for Anthos on Google Cloud.

Deploy the container image using the following command:

gcloud run deploy SERVICE-NAME \
    --image \
    --platform managed


  • SERVICE-NAME with your chosen service name.
  • PROJECT with your Google Cloud project ID.
  • IMAGE with your image name.

Notice that the container image is deployed to the service and region (Cloud Run) or cluster (Cloud Run for Anthos on Google Cloud) that you configured previously under Setting up gcloud.

If deploying to Cloud Run, you will be prompted to allow unauthenticated invocations. If you want immediate access to your service, respond y, "Yes" to the "allow unauthenticated" prompt. If your service is private and requests must include authentication, respond n, "No" to the "allow unauthenticated" prompt.

If your service is public, use the URL displayed after a successful deployment to visit your deployed container. If your service is private, use the following curl command to invoke your service:

curl -H "Authorization: bearer $(gcloud auth print-identity-token)" SERVICE_URL

If deployed to Cloud Run for Anthos on Google Cloud, see Accessing your deployed service.

Cleaning up

If you created a new project for this tutorial, delete the project. If you used an existing project and wish to keep it without the changes added in this tutorial, delete resources created for the tutorial.

Deleting the project

The easiest way to eliminate billing is to delete the project that you created for the tutorial.

To delete the project:

  1. In the Cloud Console, go to the Manage resources page.

    Go to the Manage resources page

  2. In the project list, select the project that you want to delete and then click Delete .
  3. In the dialog, type the project ID and then click Shut down to delete the project.

Deleting tutorial resources

  1. Delete the Cloud Run service you deployed in this tutorial:

    gcloud run services delete SERVICE-NAME

    Where SERVICE-NAME is your chosen service name.

    You can also delete Cloud Run services from the Google Cloud Console.

  2. Remove the gcloud default configurations you added during tutorial setup.

    If you use Cloud Run (fully managed), remove the region setting:

     gcloud config unset run/region

    If you use Cloud Run for Anthos on Google Cloud, remove the cluster configuration:

     gcloud config unset run/cluster run/cluster
     gcloud config unset run/cluster run/cluster_location
  3. Remove the project configuration:

     gcloud config unset project

What's next