Java Quick Start
This document describes how to containerize a Java application from scratch and deploy it to CloudBase Cloud Run.
Sample code:
https://github.com/TencentCloudBase/cloudbase-examples/tree/master/cloudbaserun/java
or deploy to cloud hosting with one click:
Step 1: Write a Basic Application (Create a Spring Boot Project)
Ensure that curl and unzip are installed. Execute the following command in the terminal to quickly generate a Spring Boot Web project:
curl https://start.spring.io/starter.zip \
-d type=maven-project \
-d dependencies=web \
-d javaVersion=1.8 \
-d bootVersion=2.3.3.RELEASE \
-d name=helloworld \
-d artifactId=helloworld \
-d baseDir=helloworld \
-o helloworld.zip
unzip helloworld.zip
cd helloworld
The above command will create a Spring Boot project.
If you need to use the above curl command on Windows systems, you need one of the following command lines:
Alternatively, optionally use Spring Initializr (preloaded configuration) to generate the project:
Update the content of src/main/java/com/example/helloworld/HelloworldApplication.java as follows:
package com.example.helloworld;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class HelloworldApplication {
@RestController
class HelloworldController {
@GetMapping("/")
String hello() {
return "Hello World!";
}
}
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}
Set the server port to 80 in src/main/resources/application.properties:
server.port=80
The above code will create a basic Web server and listen on port 80.
Step 2: Containerize the Application
Create a file named Dockerfile in the project root directory with the following content:
# Use the official maven/Java 8 image as the building environment.
# https://hub.docker.com/_/maven
FROM maven:3.6-jdk-11 as builder
# Copy the code into the container.
WORKDIR /app
COPY pom.xml .
COPY src ./src
# Build the project.
RUN mvn package -DskipTests
# Use AdoptOpenJDK as the base image.
# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk11:alpine-slim
# Place the jar into the container.
COPY /app/target/helloworld-*.jar /helloworld.jar
# Start the service.
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/helloworld.jar"]
Add a .dockerignore file to exclude files from the container image:
Dockerfile
.dockerignore
target/
README.md
HELP.md
Step 3 (Optional): Local Build and Run
If you have Docker installed locally, you can run the following command to build the Docker image locally:
docker build -t helloworld-java .
After a successful build, run docker images to view the built image:
REPOSITORY TAG IMAGE ID CREATED SIZE
helloworld-java latest c994813b495b 8 seconds ago 271MB
You can then upload this image to your image repository.
Run the following command to start the container:
docker run -p 80:80 helloworld-python
Access http://localhost, you should see the "Hello World!" output.
Step 4: Deploy to CloudBase Cloud Hosting
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. For more deployment methods, refer to Deploying Services.
You can refer to the following example Java framework projects: