Deploy Spring Boot Project to Azure App Service
Nowadays, most of the web applications are hosted on cloud because it is more cost-effective, safe, reliable, high speed etc. In this ariticle, I will show you how to deploy a simple spring boot project to the Azure App Service
at no cost. Before that, you need to create a Azure account first. By default, Azure
grants everyone two hundred dollars credit! Next, we create a very simple Spring Boot project from spring initializer and add the Spring Web
dependency.
Complete pom.xml example #
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.caizhenhua</groupId>
<artifactId>azure.appservice.demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>azure.appservice.demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>16</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Add a test endpoint #
Next we add a simple endpoint that simply return Hello World
message.
package com.caizhenhua.azure.appservice.demo.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/hello")
public String hello() {
return "Hello World";
}
}
Create Dockerfile #
To deploy our spring boot application, we can either use code or container. For this tutorial, I will use container because we dont have to configure any hardware settings ourselves. Therefore, you need to install the Docker first. After that, we need to build our project and create an execution jar file. Next copy the following Dockerfile
and build the image by the following command.
docker build -t yourdockerhubusername/simplespringboot .
Docker Hub
is a place that helps developers to manage containers. It also offers one free private repository, therefore we can push the image to Docker Hub
such that Azure
can pull and build our image and be able to run the container. Note that you can use another container registry like Azure Container Registry
(not free), just replace the name yourdockerhubusername
.
FROM adoptopenjdk/openjdk16:alpine
EXPOSE 80
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Push your newly created image #
You can push the image by Docker Desktop
or using the following command.
docker push yourdockerhubusername/simplespringboot:latest
Use Azure Portal for deployment #
Now you can go to the Azure Portal, select App Service
and create your web app! The process is straightforward, make sure you select test plan and clean up (delete) your resources (App Service
& App Service Plan
) to avoid further charges. To check the deployment status, navigate to the Deployment Center
and select Logs
. Deployment is successful if you can see logs similar to 2021-11-23T06:17:55.509Z INFO - Container xxx for site xxx initialized successfully and is ready to serve requests.
Enable SSH #
By default, SSH is not enabled for custom containers. We have to do some settings:
Dockerfile
FROM adoptopenjdk/openjdk16:alpine
# Install OpenSSH and set the password for root to "Docker!". In this example, "apk add" is the install instruction for an Alpine Linux-based image.
RUN apk add openssh \
&& echo "root:Docker!" | chpasswd
# Copy the sshd_config file to the /etc/ssh/ directory
COPY sshd_config /etc/ssh/
# Copy and configure the ssh_setup file
RUN mkdir -p /tmp
COPY ssh_setup.sh /tmp
RUN chmod +x /tmp/ssh_setup.sh \
&& (sleep 1;/tmp/ssh_setup.sh 2>&1 > /dev/null)
EXPOSE 80 2222
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
COPY start.sh /scripts/start.sh
RUN ["chmod", "+x", "/scripts/start.sh"]
ENTRYPOINT ["/scripts/start.sh"]
sshd_config
Port 2222
ListenAddress 0.0.0.0
LoginGraceTime 180
X11Forwarding yes
Ciphers aes128-cbc,3des-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr
MACs hmac-sha1,hmac-sha1-96
StrictModes yes
SyslogFacility DAEMON
PasswordAuthentication yes
PermitEmptyPasswords no
PermitRootLogin yes
Subsystem sftp internal-sftp
ssh_setup.sh
#!/bin/sh
if [ ! -f "/etc/ssh/ssh_host_rsa_key" ]; then
# generate fresh rsa key
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
fi
if [ ! -f "/etc/ssh/ssh_host_dsa_key" ]; then
# generate fresh dsa key
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
fi
if [ ! -f "/etc/ssh/ssh_host_ecdsa_key" ]; then
# generate fresh ecdsa key
ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key -N '' -t dsa
fi
if [ ! -f "/etc/ssh/ssh_host_ed25519_key" ]; then
# generate fresh ecdsa key
ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N '' -t dsa
fi
#prepare run dir
if [ ! -d "/var/run/sshd" ]; then
mkdir -p /var/run/sshd
fi
start.sh
#!/bin/sh
/usr/sbin/sshd
java -jar /app.jar
Please note that the above settings only works for the Alpine Linux
linux distribution only, hence you have to update the settings to enable the SSH server if you switch to another Linux distribution such as CentOS or Ubuntu.
- Previous: Install SonarQube on Mac
- Next: 什么是Closure闭包,Java支持闭包吗?