This article is contributed. See the original author and article here.

In this article we will create a Docker image from a Java project using Azure Container Registry and then it will be deployed in a Docker compatible hosting environment, for instance Azure Container App.

For this process it is required:

  • JDK 1.8+

  • Maven

  • Azure CLI

  • GIT

And the following Azure resources:

  • Azure Container Registry

  • Azure Container App. This resource can be changed by other container hosting service, such as Azure App Service, Azure Functions or Azure Kubernetes Service.

These Azure resources can be create using the following az cli commands:


# create a resource group to hold resources for the demo
az group create --location $LOCATION --name $RESOURCE_GROUP
# create an Azure Container Registry (ACR) to hold the images for the demo
az acr create --resource-group $RESOURCE_GROUP --name $ACR_NAME --sku Standard --location $LOCATION

# register container apps extension
az extension add --name containerapp --upgrade
# register Microsoft.App namespace provider
az provider register --namespace Microsoft.App
# create an azure container app environment
az containerapp env create 
    --resource-group $RESOURCE_GROUP 
    --location $LOCATION

# Create a user managed identity and assign AcrPull role on the ACR.
USER_IDENTITY=$(az identity create -g $RESOURCE_GROUP -n $CONTAINERAPPS_NAME --location $LOCATION --query clientId -o tsv)
ACR_RESOURCEID=$(az acr show --name $ACR_NAME --resource-group $RESOURCE_GROUP --query "id" --output tsv)
az role assignment create 
    --assignee "$USER_IDENTITY" --role AcrPull --scope "$ACR_RESOURCEID"

# container app will be created once the image is pushed to the ACR


Let’s take a sample application, for instance the well known spring pet clinic.


git clone
cd spring-petclinic


Then create a Dockerfile. For demo purposes it will be as simple as possible.


FROM openjdk:8-jdk-slim
# takes the jar file as an argument
# assumes the application entry port is 8080

# The application's jar file

# Add the application's jar to the container
ADD ${JAR_FILE} app.jar

# Run the jar file
ENTRYPOINT ["java","-jar","/app.jar"]


To build it locally the following command would be used:


docker build -t 
    -f Dockerfile 
    --build-arg ARTIFACT_NAME=target/spring-petclinic-2.7.0-SNAPSHOT.jar 


To build it in Azure Container Registry the following command would be used instead:


az acr build 
        --resource-group rg-spring-petclinic 
        --registry myacr 
        --image spring-petclinic:2.7.0 
        --build-arg ARTIFACT_NAME=target/spring-petclinic-2.7.0-SNAPSHOT.jar 


Instead of execute it manually, this command can be integrated as part of the maven build cycle. To perform this action, we will use 

org.codehaus.mojo:exec-maven-plugin. This plugin allows to execute system or Java programs. It will be used to execute the previous az cli command. To include as part of the build lifecycle it will be created a new maven profile.


To execute this process just execute the following maven goal with the new profile.


The resource group and the Azure Container Registry are passed as environment variables, but the can be defined as maven parameters or just hardcoding.

Now the image already exists in Azure Container Registry.


Now it’s time to create the Container App.

# Create the container app
az containerapp create 
    --resource-group $RESOURCE_GROUP 
    --container-name spring-petclinic-container 
    --user-assigned ${CONTAINERAPPS_NAME} 
    --registry-server $ 
    --image $ 
    --ingress external 
    --target-port 8080 
    --cpu 1 
    --memory 2 

Now the application is deployed and running on Azure Container Apps.



You can find the source code for this application here.


Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.