Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
You can use this Quickstart to build container images of Java Spring Boot app and push it to Azure Container Registry using Maven and Jib. Maven and Jib are one way of using developer tooling to interact with an Azure container registry.
- An Azure subscription; Sign up for a trial subscription or activate MSDN subscriber benefits if you don't already have an Azure subscription.
- A supported Java Development Kit (JDK); For more information on available JDKs when developing on Azure, see Java support on Azure and Azure Stack.
- The Azure CLI.
- The Apache's Maven build tool (Version 3 or above).
- A Git client.
- A Docker client.
- The ACR Docker credential helper.
The following steps walk you through building a containerized Java Spring Boot web application and testing it locally.
From the command prompt, use the following command to clone the Spring Boot on Docker Getting Started sample project.
git clone https://github.com/spring-guides/gs-spring-boot-docker.git
Change directory to the complete project.
cd gs-spring-boot-docker/complete
Use Maven to build and run the sample app.
mvn package spring-boot:run
Test the web app by browsing to
http://localhost:8080
, or with the followingcurl
command:curl http://localhost:8080
You should see the following message displayed: Hello Docker World
Next, you'll create an Azure resource group and your ACR using the following steps:
Log in to your Azure account by using the following command:
az cloud set -n AzureChinaCloud az login # az cloud set -n AzureCloud //means return to Public Azure.
Specify the Azure subscription to use:
az account set -s <subscription ID>
Create a resource group for the Azure resources used in this tutorial. In the following command, be sure to replace the placeholders with your own resource name and a location such as
chinaeast2
.az group create \ --name=<your resource group name> \ --location=<location>
Create a private Azure container registry in the resource group, using the following command. Be sure to replace the placeholders with actual values. The tutorial pushes the sample app as a Docker image to this registry in later steps.
az acr create \ --resource-group <your resource group name> \ --location <location> \ --name <your registry name> \ --sku Basic
Finally, you'll update your project configuration and use the command prompt to build and deploy your image.
Note
To log in the Azure container registry that you just created, you will need to have the Docker daemon running. To install Docker on your machine, here is the official Docker documentation.
Log in to your Azure Container Registry from the Azure CLI using the following command. Be sure to replace the placeholder with your own registry name.
az config set defaults.acr=<your registry name> az acr login
The
az config
command sets the default registry name to use withaz acr
commands.Navigate to the completed project directory for your Spring Boot application (for example, "C:\SpringBoot\gs-spring-boot-docker\complete" or "/users/robert/SpringBoot/gs-spring-boot-docker/complete"), and open the pom.xml file with a text editor.
Update the
<properties>
collection in the pom.xml file with the following XML. Replace the placeholder with your registry name, and add a<jib-maven-plugin.version>
property with value2.2.0
, or a newer version of the jib-maven-plugin.<properties> <docker.image.prefix><your registry name>.azurecr.cn</docker.image.prefix> <java.version>1.8</java.version> <jib-maven-plugin.version>2.2.0</jib-maven-plugin.version> </properties>
Update the
<plugins>
collection in the pom.xml file so that the<plugin>
element contains and an entry for thejib-maven-plugin
, as shown in the following example. Note that we are using a base image from the Microsoft Container Registry (MCR):mcr.microsoft.com/openjdk/jdk:11-ubuntu
, which contains an officially supported JDK for Azure. For other MCR base images with officially supported JDKs, see Install the Azure Build of OpenJDK.<plugin> <artifactId>jib-maven-plugin</artifactId> <groupId>com.google.cloud.tools</groupId> <version>${jib-maven-plugin.version}</version> <configuration> <from> <image>mcr.microsoft.com/openjdk/jdk:11-ubuntu</image> </from> <to> <image>${docker.image.prefix}/${project.artifactId}</image> </to> </configuration> </plugin>
Navigate to the complete project directory for your Spring Boot application and run the following command to build the image and push the image to the registry:
az acr login && mvn compile jib:build
Note
For security reasons, the credential created by az acr login
is valid for 1 hour only. If you receive a 401 Unauthorized error, you can run the az acr login -n <your registry name>
command again to reauthenticate.
Congratulations! Now you have your containerized Java App build on Azure supported JDK pushed to your ACR. You can now test the image by deploying it to Azure App Service, or pulling it to local with command (replacing the placeholder):
docker pull <your registry name>.azurecr.cn/gs-spring-boot-docker
For other versions of the official Microsoft-supported Java base images, see:
To learn more about Spring and Azure, continue to the Spring on Azure documentation center.
For more information, see the following resources: