تحرير

Deploy a Spring Boot application to Linux on Azure App Service

This tutorial shows how to use Docker to containerize your Spring Boot application and deploy your own Docker image to a Linux host in the Azure App Service.

Prerequisites

To complete the steps in this tutorial, you need the following prerequisites:

Note

Due to the virtualization requirements of this tutorial, you can't follow the steps in this article on a virtual machine. You must use a physical computer with virtualization features enabled.

Create the Spring Boot on Docker Getting Started web app

The following steps walk through the steps that are required to create a simple Spring Boot web application and test it locally.

  1. Open a command prompt and create a local directory to hold your application, and change to that directory; for example:

    mkdir SpringBoot
    cd SpringBoot
    
  2. Clone the Spring Boot on Docker Getting Started sample project into the directory you created; for example:

    git clone https://github.com/spring-guides/gs-spring-boot-docker.git
    
  3. Change directory to the completed project; for example:

    cd gs-spring-boot-docker/complete
    
  4. Build the JAR file using Maven; for example:

    mvn package
    
  5. Once the web app is created, change directory to the target directory where the JAR file is located and start the web app; for example:

    cd target
    java -jar spring-boot-docker-complete-0.0.1-SNAPSHOT.jar --server.port=80
    
  6. Test the web app by browsing to it locally using a web browser. For example, if you have curl available and you configured the Tomcat server to run on port 80:

    curl http://localhost
    
  7. You see the following message displayed: Hello Docker World

    Browse Sample App Locally

Create an Azure Container Registry to use as a private Docker registry

The following steps walk through using the Azure portal to create an Azure Container Registry.

Note

If you want to use the Azure CLI instead of the Azure portal, follow the steps in Create a private Docker container registry using the Azure CLI 2.0.

  1. Browse to the Azure portal and sign in.

    After you sign in to your account on the Azure portal, follow the steps in the Create a private Docker container registry using the Azure portal article. The following steps paraphrase those instructions for the sake of expediency.

  2. Select the menu icon for New, select Containers, and then select Azure Container Registry.

    Create a new Azure Container Registry

  3. When the Create container registry page appears, enter Registry name, Subscription, Resource group, and Location. Then select Create.

    Configure Azure Container Registry settings

  4. Select Access keys under Settings and turn on the Enabled switch for the Admin user.

    Enable admin user UI in the Azure portal

Configure Maven to build image to your Azure Container Registry

  1. Go 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.

  2. Update the <properties> collection in the pom.xml file with the latest version of jib-maven-plugin, sign-in server value, and access settings for your Azure Container Registry from the previous section of this tutorial. For example:

    <properties>
       <jib-maven-plugin.version>3.2.0</jib-maven-plugin.version>
       <docker.image.prefix>wingtiptoysregistry.azurecr.io</docker.image.prefix>
       <java.version>1.8</java.version>
    </properties>
    
  3. Add jib-maven-plugin to the <plugins> collection in the pom.xml file. This example uses version 3.2.0.

    Specify the base image at <from>/<image>, here mcr.microsoft.com/openjdk/jdk:11-ubuntu. Specify the name of the final image to build from the base in <to>/<image>.

    Authentication {docker.image.prefix} is the Sign-in server on the registry page shown previously. The {project.artifactId} is the name and version number of the JAR file from the first Maven build of the project.

    <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>
    
  4. Go to the completed project directory for your Spring Boot application and run the following command to rebuild the application and push the container to your Azure Container Registry:

    az acr login --name wingtiptoysregistry && mvn compile jib:build
    

Note

The command az acr login ... tries to sign in to Azure Container Registry. Otherwise, you need to provide <username> and <password> for jib-maven-plugin. To learn more, see Authentication Methods in jib.

When you use Jib to push your image to the Azure Container Registry, the image doesn't use the Dockerfile. For more information, see this article.

Create a web app on Linux on Azure App Service by using your container image

  1. Browse to the Azure portal and sign in.

  2. Select the menu icon for Create a resource, select Compute, and then select Web App.

    Create a new web app in the Azure portal

  3. When the Web App on Linux page appears, enter the following information:

    • Choose your Subscription from the drop-down list.

    • Choose an existing Resource Group, or specify a name to create a new resource group.

    • Enter a unique name for the App name; for example: wingtiptoyslinux.

    • Specify Docker Container to Publish.

    • For the Operating System field, choose Linux.

    • Select Region.

    • Accept Linux Plan and choose an existing App Service Plan, or select Create new to create a new app service plan.

    • Select Next: Docker.

    Click the Next: Docker button to proceed.

    On the Web App page select Docker, and enter the following information:

    • Select Single Container.

    • Registry: Choose your container, for example: wingtiptoysregistry.

    • Image: Select the image created previously, for example: spring-boot-docker.

    • Tag: Choose the tag for the image, for example: latest.

    • Startup Command: Keep it blank since the image already has the start up command.

    After you enter all of the preceding information, select Review + create.

    Finish by selecting Review + Create.

    • Select Review + create.

Review the information and select Create.

When the deployment finishes, select Go to resource. The deployment page displays the URL to access the application.

Get URL of deployment

Note

Azure automatically maps Internet requests to the embedded Tomcat server that runs on port 80. However, if you configure your embedded Tomcat server to run on port 8080 or a custom port, you need to add an environment variable to your web app that defines the port for your embedded Tomcat server. To add the environment variable, use the following steps:

  1. Browse to the Azure portal and sign in.

  2. Select the icon for Web Apps, and select your app from the App Services page.

  3. Select Configuration in the left navigation pane.

  4. In the Application settings section, add a new setting named WEBSITES_PORT and enter your custom port number for the value.

  5. Select OK. Then select Save.

Saving a custom port number in the Azure portal

Clean up resources

When you no longer need the resources, use the Azure portal to delete them. Deleting the resources helps you avoid unexpected charges.

Next steps

To learn more about Spring and Azure, continue to the Spring on Azure documentation center.

See also

For more information about using Spring Boot applications on Azure, see the following articles:

For more information about using Azure with Java, see the Azure for Java Developers and the Working with Azure DevOps and Java.

For further details about the Spring Boot on Docker sample project, see Spring Boot on Docker Getting Started.

For help with getting started with your own Spring Boot applications, see the Spring Initializr at https://start.spring.io/.

For more information about getting started with creating a simple Spring Boot application, see the Spring Initializr at https://start.spring.io/.

For more examples of how to use custom Docker images with Azure, see Using a custom Docker image for Azure Web App on Linux.