Skip to main content

Spring Boot

Spring Boot is an out-of-the-box Java framework based on the Spring ecosystem, simplifying enterprise application development. With embedded Web servers, auto-configuration, and Starter dependencies, it enables rapid building of standalone, deployable microservices or monolithic applications without complex XML configuration. This significantly improves development efficiency, making it ideal for modern cloud-native and distributed systems.

This guide describes how to deploy the Spring Boot application on Tencent CloudBase through multiple approaches:

Creating a Spring Boot Application

Note: If you already have a Spring Boot application, you can skip this step.

To create a Spring Boot application, ensure that the JDK is installed on your machine.

Go to start.spring.io to initialize a new Spring Boot application. Select the following options to customize and generate your application.

  • Project: Maven This guide describes how to deploy the Spring Boot application on Tencent CloudBase through multiple approaches:
  • Spring Boot: 3.3.4 This guide describes how to deploy the Spring Boot application on Tencent CloudBase through multiple approaches:
    • Group: com.tencent
    • Artifact: cloudrun
    • Name: cloudrun
    • Description: Demo project for Spring Boot
    • Package name: com.tencent.cloudrun
    • Packaging: Jar
    • Java: 17
  • Dependencies:
    • Click the "Add Dependency" button and search for Spring Boot. Select it.

Click the "CREATE" button, download the compressed file, and extract it to a folder on your machine.

Configuring Dockerfile

Create a Dockerfile file in the root directory of the Spring Boot application with the following content:

Note: maven version and jre version need to be modified according to the actual situation. The name of cloudrun-1.0-SNAPSHOT.jar should be modified according to the actual situation. The settings.xml file is required in the root directory.

FROM maven:3.6.0-jdk-17-slim as build

# Specify the working directory during the build process
WORKDIR /app

# Copy all files from the src directory to the src subdirectory in the working directory (excluding files specified in .gitignore/.dockerignore)
COPY src /app/src

# Copy the pom.xml file to the working directory
COPY settings.xml pom.xml /app/

# Execute the code compilation command
# Customize settings.xml and use a domestic mirror source to improve download speed.
RUN mvn -s /app/settings.xml -f /app/pom.xml clean package

# Select the runtime base image
FROM alpine:3.13

# Install dependencies. If you need additional dependencies, search in the alpine package management (https://pkgs.alpinelinux.org/packages?name=php8*imagick*&branch=v3.13).
# Use a domestic mirror source to improve download speed.
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tencent.com/g' /etc/apk/repositories \
&& apk add --update --no-cache openjdk17-jre-base \
&& rm -f /var/cache/apk/*

# The container's default time zone is UTC. To use Shanghai time, enable the following time zone setting command.
# RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone

# Using the HTTPS Protocol to Access Container Cloud for Certificate Installation Invocation
RUN apk add ca-certificates

# Specify the working directory at runtime
WORKDIR /app

# Copy the built jar package to the runtime directory
COPY --from=build /app/target/*.jar .

# Install composer
# The port here must match the port specified in "Service Settings" - "Pipeline" and during "Manual Code Package Upload" deployment, otherwise the deployment will fail.
EXPOSE 80

# Execute startup command.
# Writing multiple independent CMD commands is incorrect! Only the last CMD command will be executed, and all previous ones will be ignored, causing application errors.
# Refer to [Docker official documentation on CMD command](https://docs.docker.com/engine/reference/builder/#cmd)
CMD ["java", "-jar", "/app/cloudrun-1.0-SNAPSHOT.jar"]

With the above changes, your springboot application will be deployable to Tencent Cloud Hosting!

Deployment to Cloud Hosting

Cloud Hosting provides multiple deployment methods to deploy your application:

Deploy via Console

Open Tencent CloudBase, click Deploy via local code -> enter the service name -> select Upload code package for the deployment method -> choose Folder for the code package type -> select the cloudrun-gin directory to upload -> enter port 8080 -> click Create and wait for the creation to complete.

Deploying via cli

If you have installed CloudBase CLI, you can use the following command in the project directory to deploy the application to CloudBase Cloud Hosting:

tcb cloudrun deploy

After entering the environment and service names, the CLI will automatically package the application image and deploy it to Cloud Hosting.

Besides manual deployment, you can also install the aforementioned applications with one click:

One-Click Deployment from Template

One-Click Deployment from github