Skip to content

Latest commit

 

History

History
296 lines (214 loc) · 12.6 KB

deploy-spring-boot-java-app-on-linux.md

File metadata and controls

296 lines (214 loc) · 12.6 KB
title description services documentationcenter author manager editor ms.assetid ms.author ms.date ms.devlang ms.service ms.tgt_pltfrm ms.topic ms.workload ms.custom
Deploy a Spring Boot Web App on Azure App Service for Container
This tutorial walks you though the steps to deploy a Spring Boot application as a Linux web app on Microsoft Azure.
azure app service
java
bmitchell287
douge
brendm
11/12/2019
java
app-service
multiple
article
web
mvc

Deploy a Spring Boot application on Azure App Service for Container

This tutorial walks you through using Docker to containerize your Spring Boot application and deploy your own docker image to a Linux host in the Azure App Service.

Prerequisites

In order to complete the steps in this tutorial, you need to have the following prerequisites:

Note

Due to the virtualization requirements of this tutorial, you cannot 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 you 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:

    md C:\SpringBoot
    cd C:\SpringBoot
    

    -- or --

    md /users/robert/SpringBoot
    cd /users/robert/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 has been created, change directory to the target directory where the JAR file is located and start the web app; for example:

    cd target
    java -jar gs-spring-boot-docker-0.1.0.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 should 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 you 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.

    Once you have signed in to your account on the Azure portal, you can follow the steps in the Create a private Docker container registry using the Azure portal article, which are paraphrased in the following steps for the sake of expediency.

  2. Click the menu icon for + New, then click Containers, and then click Azure Container Registry.

    Create a new Azure Container Registry

  3. When the information page for the Azure Container Registry template is displayed, click Create.

    Create a new Azure Container Registry

  4. When the Create container registry page is displayed, enter your Registry name and Resource group, choose Enable for the Admin user, and then click Create.

    Configure Azure Container Registry settings

  5. Once your container registry has been created, navigate to your container registry in the Azure portal, and then click Access Keys. Take note of the username and password for the next steps.

    Azure Container Registry access keys

Configure Maven to use your Azure Container Registry access keys

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

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

    <properties>
       <jib-maven-plugin.version>1.7.0</jib-maven-plugin.version>
       <docker.image.prefix>wingtiptoysregistry.azurecr.io</docker.image.prefix>
       <java.version>1.8</java.version>
       <username>wingtiptoysregistry</username>
       <password>{put your Azure Container Registry access key here}</password>
    </properties>
  3. Add jib-maven-plugin to the <plugins> collection in the pom.xml file, specify the base image at <from>/<image> and final image name <to>/<image>, specify the username and password from previous section at <to>/<auth>. For example:

    <plugin>
      <artifactId>jib-maven-plugin</artifactId>
      <groupId>com.google.cloud.tools</groupId>
      <version>${jib-maven-plugin.version}</version>
      <configuration>
         <from>
             <image>openjdk:8-jre-alpine</image>
         </from>
         <to>
             <image>${docker.image.prefix}/${project.artifactId}</image>
             <auth>
                <username>${username}</username>
                <password>${password}</password>
             </auth>
         </to>
      </configuration>
    </plugin>
  4. Navigate 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:

    mvn compile jib:build

Note

When you are using Jib to push your image to the Azure Container Registry, the image will not honor Dockerfile, see this document for details.

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

  1. Browse to the Azure portal and sign in.

  2. Click the menu icon for + Create a resource, then click Web, and then click Web App for Containers.

    Create a new web app in the Azure portal

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

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

    b. Choose your Subscription from the drop-down list.

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

    d. Choose Linux as the OS.

    e. Click App Service plan/Location and choose an existing app service plan, or click Create new to create a new app service plan.

    f. Click Configure container and enter the following information:

    • Choose Single Container and Azure Container Registry.

    • Registry: Choose your container name created earlier; for example: "wingtiptoysregistry"

    • Image: Choose the image name; for example: "gs-spring-boot-docker"

    • Tag: Choose the tag for the image; for example: "latest"

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

    e. Once you have entered all of the above information, click Apply.

    Configure web app settings

  4. Click Create.

Note

Azure will automatically map Internet requests to embedded Tomcat server that is running on the standard ports of 80 or 8080. However, if you configured your embedded Tomcat server to run on a custom port, you need to add an environment variable to your web app that defines the port for your embedded Tomcat server. To do so, use the following steps:

  1. Browse to the Azure portal and sign in.

  2. Click the icon for App Services, and select your web app from the list.

  3. Click Configuration. (Item #1 in the image below.)

  4. In the Application settings section, add a new setting named PORT and enter your custom port number for the value. (Item #2, #3, #4 in the image below.)

  5. Click Save. (Item #5 in the image below.)

Saving a custom port number in the Azure portal

Next steps

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

[!div class="nextstepaction"] Spring on Azure

Additional Resources

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 additional examples for how to use custom Docker images with Azure, see Using a custom Docker image for Azure Web App on Linux.