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 |
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.
In order to complete the steps in this tutorial, you need to have the following prerequisites:
- An Azure subscription; if you don't already have an Azure subscription, you can activate your MSDN subscriber benefits or sign up for a free Azure account.
- The Azure Command-Line Interface (CLI).
- A supported Java Development Kit (JDK). For more information about the JDKs available for use when developing on Azure, see https://aka.ms/azure-jdks.
- Apache's Maven build tool (Version 3).
- A Git client.
- A Docker client.
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.
The following steps walk you through the steps that are required to create a simple Spring Boot web application and test it locally.
-
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
-
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
-
Change directory to the completed project; for example:
cd gs-spring-boot-docker/complete
-
Build the JAR file using Maven; for example:
mvn package
-
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
-
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
-
You should see the following message displayed: Hello Docker World
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.
-
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.
-
Click the menu icon for + New, then click Containers, and then click Azure Container Registry.
-
When the information page for the Azure Container Registry template is displayed, click Create.
-
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.
-
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.
-
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 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>
-
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>
-
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.
-
Browse to the Azure portal and sign in.
-
Click the menu icon for + Create a resource, then click Web, and then click Web App for Containers.
-
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.
-
-
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:
-
Browse to the Azure portal and sign in.
-
Click the icon for App Services, and select your web app from the list.
-
Click Configuration. (Item #1 in the image below.)
-
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.)
-
Click Save. (Item #5 in the image below.)
To learn more about Spring and Azure, continue to the Spring on Azure documentation center.
[!div class="nextstepaction"] Spring on Azure
For more information about using Spring Boot applications on Azure, see the following articles:
- Deploy a Spring Boot Application to the Azure App Service
- Deploy a Spring Boot Application on a Kubernetes Cluster in the Azure Container Service
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.