إشعار
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تسجيل الدخول أو تغيير الدلائل.
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تغيير الدلائل.
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:
- An Azure subscription; if you don't already have an Azure subscription, 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 Java support on Azure and Azure Stack.
- Apache Maven build tool (Version 3).
- A Git client.
- A Docker client.
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.
Open a command prompt and create a local directory to hold your application, and change to that directory; for example:
mkdir SpringBoot cd SpringBootClone 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.gitChange directory to the completed project; for example:
cd gs-spring-boot-docker/completeBuild the JAR file using Maven; for example:
mvn packageOnce the web app is created, change directory to the
targetdirectory 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=80Test 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://localhostYou see the following message displayed: Hello Docker World

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.
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.
Select the menu icon for New, select Containers, and then select Azure Container Registry.

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

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

Configure Maven to build image to your Azure Container Registry
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.
Update the
<properties>collection in the pom.xml file with the latest version ofjib-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>Add
jib-maven-pluginto the<plugins>collection in the pom.xml file. This example uses version 3.2.0.Specify the base image at
<from>/<image>, heremcr.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>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
Browse to the Azure portal and sign in.
Select the menu icon for Create a resource, select Compute, and then select Web App.

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

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.

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

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:
Browse to the Azure portal and sign in.
Select the icon for Web Apps, and select your app from the App Services page.
Select Configuration in the left navigation pane.
In the Application settings section, add a new setting named WEBSITES_PORT and enter your custom port number for the value.
Select OK. Then select Save.

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.