Java Quick Start
This document describes how to containerize a Java application from scratch and deploy it to CloudBase Cloud Hosting.
Sample code:
https://github.com/TencentCloudBase/cloudbase-examples/tree/master/cloudbaserun/java
Or deploy to cloud hosting with one click:
Step 1: Write the Basic Application (Create a Spring Boot Project)
Ensure that curl and unzip are installed. Run 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.
To use the above curl command on Windows systems, you need one of the following command-line tools:
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);
}
}
In src/main/resources/application.properties
, set the server port to 80
:
server.port=80
The above code creates a basic Web server and listens on port 80
.
Step 2: Containerize the Application
In the project root directory, create a file named Dockerfile
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): Build and Run Locally
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
Then you can 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 Run
If you have already installed the 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.
For Java framework project samples, you can refer to: