Docker image SpringBoot Jar package

Keywords: Docker Tomcat JDK SpringBoot

Docker image SpringBoot Jar package

Preface

Let's start with a general process. I'm building a project with SpringBoot, typing it directly into a Jar package, running on port 8082 on Linux, and changing the name of the Jar package to docker-package.jar, upload the Jar package, create a Dockerfile in the same directory (without a suffix), and then use the docker build [OPTIONS] PATH directive to mirror the Jar package as a Docker.
Let's get started.

Pack Project

The project code is simple, just provide a Get interface to see the calls.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/docker")
@SpringBootApplication
public class DockerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DockerApplication.class, args);
    }

    @GetMapping("/get")
    public String getData() {
        return "Call succeeded:" + System.currentTimeMillis();
    }

}

I added a Get interface to my project: /docker/get
Avoid port conflicts and change project port to 8082
Application.propertiesThe contents are as follows:

server.port=8082

Then I specify the Jar package name as: docker-package, just need toPom.xmlConfiguring finalName in is OK.

<build>
	   <plugins>
	       <plugin>
	           <groupId>org.springframework.boot</groupId>
	           <artifactId>spring-boot-maven-plugin</artifactId>
	       </plugin>
	   </plugins>
	   <!-- Appoint jar Package Name -->
	   <finalName>docker-package</finalName>
</build>

Project package, get docker-package.jar

Making Docker Mirrors

We will docker-Package.jarUpload to Linux and create a Dockerfile (without a suffix) in the same directory
Dockerfile is roughly as follows:

[root@instance-g1almmmy docker]# cat Dockerfile 
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY docker-package.jar docker-package.jar
ENTRYPOINT ["java","-jar","/docker-package.jar"]

FROMOpenjdk:8-jdk-alpine: UseOpenjdk:8-jdk-alpineEnvironment Creation
VOLUME/tmp: Defines an anonymous data volume.If you forget to mount the data volume when you start the container, it will automatically mount to an anonymous volume.
COPY docker-Package.jarDocker-Package.jar: Copy instructions to copy files from a context directory or directory to a specified path in a container.(preceding docker-Package.jarIs the path on the host, followed by docker-Package.jarIs the path copied to the container)
ENTRYPOINT ["java", -jar", /docker-Package.jar"]: When executing docker run, you can specify the parameters required for ENTRYPOINT to run.

More relevant parameters can be seen: Docker Dockerfile

Run instructions: docker build-t docker-Package:v0617.
Notice that there is also a "."
-t: The name and label of the mirror, usuallyName:tag
The following output indicates that the mirror was successfully made

Sending build context to Docker daemon   55.1MB
Step 1/4 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/4 : VOLUME /tmp
 ---> Using cache
 ---> f2dea56e2ab9
Step 3/4 : COPY docker-package.jar docker-package.jar
 ---> 44e15cb5c397
Removing intermediate container bb38d5b87128
Step 4/4 : ENTRYPOINT java -jar /docker-package.jar
 ---> Running in bc8524326d20
 ---> e023becb7df6
Removing intermediate container bc8524326d20
Successfully built e023becb7df6
Successfully tagged docker-package:v0617

View and run Docker images

View docker image

[root@instance-g1almmmy docker]# docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
docker-package                                  v0617               e023becb7df6        14 minutes ago      132MB

Run docker image

[root@instance-g1almmmy docker]# docker run -d -p 8082:8082 e023becb7df6          
39afe42888d2942de572dff19b6e86786a3d0541707521318d6bb894a9009950

-d:Background running
-p: Specify run port host: container internal port
39afe42888d2942de572dff19b6e86786a3d05417521318d6bb894a9009950: Container ID, generated after successful operation, needed to view logs and ports.

View docker container log

[root@instance-g1almmmy docker]# docker logs 39afe42888d2942de572dff19b6e86786a3d0541707521318d6bb894a9009950

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.9.RELEASE)

2020-06-17 15:15:20.209  INFO 1 --- [           main] com.example.demo.DockerApplication       : Starting DockerApplication v0.0.1-SNAPSHOT on 39afe42888d2 with PID 1 (/docker-package.jar started by root in /)
2020-06-17 15:15:20.217  INFO 1 --- [           main] com.example.demo.DockerApplication       : No active profile set, falling back to default profiles: default
2020-06-17 15:15:22.869  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8082 (http)
2020-06-17 15:15:22.981  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-06-17 15:15:22.982  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.26]
2020-06-17 15:15:23.154  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-06-17 15:15:23.154  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2798 ms
2020-06-17 15:15:23.672  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-17 15:15:24.348  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''
2020-06-17 15:15:24.357  INFO 1 --- [           main] com.example.demo.DockerApplication       : Started DockerApplication in 5.408 seconds (JVM running for 6.408)

Run Successfully
View Port

docker port 39afe42888d2942de572dff19b6e86786a3d0541707521318d6bb894a9009950 
8082/tcp -> 0.0.0.0:8082

Indicates that 8082 of the docker container has been mapped
Startup Complete

Access Interface


Docker Mirroring Completed with SpringBoot Jar Package
Welcome to leave a message for me~

Posted by fsumba on Wed, 17 Jun 2020 09:41:31 -0700