尝试将Spring Boot应用程序进行容器化实验

这篇文章是ZOZO Advent Calendar 2021 #5的第23天的文章。

首先

我尝试将Spring Boot应用程序进行容器化。

另外,在Spring Boot中,从Spring Boot 2.3开始支持Cloud Native Buildpacks。因此,我尝试了使用Dockerfile和Cloud Native Buildpacks这两种方式来容器化。

容器的意思是什么?

使用容器将应用程序及其执行环境一起虚拟化的技术,使应用程序能够在独立于环境的情况下运行。

Docker 是什么?

这是一个使用容器虚拟化来开发、部署和运行应用程序的开放平台。

“Cloud Native Buildpacks是什么”

Cloud Native Buildpacks是一个工具,可以将应用程序的源代码转换为可以在任何云平台上运行的映像。

首先准备应用程序。

为了实现容器化,本次将准备一个简单的Web应用程序,只需要显示Hello World。

pom.xml 可以被重写为:骨架檔案.xml。

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>spring-docker-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>11</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-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>app</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

你好控制器.java


package com.example.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {

    @GetMapping("/")
    public String root() {
        return "hello/index";
    }
}

首页.html .html)

Hello World

安装Docker Desktop。

我們在這裡取得了這個:

Docker Desktop

使用Dockerfile进行容器化

首先,我们将使用Dockerfile进行容器化。

Dockerfile – Docker 文件

FROM maven:3.6.3-jdk-11 AS builder
WORKDIR /tmp
COPY ./src ./src
COPY ./pom.xml .
RUN mvn package
FROM adoptopenjdk/openjdk11:debianslim-jre
COPY --from=builder /tmp/target/app.jar /app/app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

以下是使用中的指令概述。

コマンド名内容FROMコンテナのベースイメージ指定WORKDIR作業ディレクトリの指定COPY追加したいファイル、ディレクトリをイメージのファイルシステム上のパスに追加RUNビルド時に任意のコマンドを実行するEXPOSEコンテナーの実行時に、所定ネットワーク上のどのポートをリッスンするかを指定ENTRYPOINTコンテナーを実行モジュールのようにして実行する設定を行う

如果您想了解上述指令的详细信息或其他指令,请查阅Dockerfile参考资料。

构建容器镜像

请使用以下命令构建容器镜像。

docker image build -t [Docker Hubのユーザー名(任意)]/[イメージ名(必須)]:[タグ(任意)] [Dockerfileパス]

我会试一试。

docker image build -t spring-docker-demo:0.0.1 ./

[+] Building 3.0s (13/13) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                               0.1s
 => => transferring dockerfile: 32B                                                                                                                                                                                                0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                  0.1s
 => => transferring context: 2B                                                                                                                                                                                                    0.0s
 => [internal] load metadata for docker.io/adoptopenjdk/openjdk11:debianslim-jre                                                                                                                                                   2.6s
 => [internal] load metadata for docker.io/library/maven:3.6.3-jdk-11                                                                                                                                                              2.7s
 => [stage-1 1/2] FROM docker.io/adoptopenjdk/openjdk11:debianslim-jre@sha256:a24a26c8d0f780b25efe8d61593e51ac35a502565d5e4b12bcce43807fb43fb2                                                                                     0.0s
 => [internal] load build context                                                                                                                                                                                                  0.0s
 => => transferring context: 711B                                                                                                                                                                                                  0.0s
 => [builder 1/5] FROM docker.io/library/maven:3.6.3-jdk-11@sha256:1d29ccf46ef2a5e64f7de3d79a63f9bcffb4dc56be0ae3daed5ca5542b38aa2d                                                                                                0.0s
 => CACHED [builder 2/5] WORKDIR /tmp                                                                                                                                                                                              0.0s
 => CACHED [builder 3/5] COPY ./src ./src                                                                                                                                                                                          0.0s
 => CACHED [builder 4/5] COPY ./pom.xml .                                                                                                                                                                                          0.0s
 => CACHED [builder 5/5] RUN mvn package                                                                                                                                                                                           0.0s
 => CACHED [stage-1 2/2] COPY --from=builder /tmp/target/app.jar /app/app.jar                                                                                                                                                      0.0s
 => exporting to image                                                                                                                                                                                                             0.0s
 => => exporting layers                                                                                                                                                                                                            0.0s
 => => writing image sha256:1f356d2363a438bf1a2a5ebf980acae48dbdbc22d0c47007331ce3c32d0c404d                                                                                                                                       0.0s
 => => naming to docker.io/library/spring-docker-demo:0.0.1

由于容器镜像已经创建完毕,接下来可以使用以下命令进行确认。

docker image ls

REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
spring-docker-demo   0.0.1     1f356d2363a4   48 minutes ago   251MB

我已确认。

开启

使用以下命令进行启动。

docker run -it -p [ホスト側のポート番号]:[コンテナのポート番号] [イメージ名]:[タグ]
docker run -it -p 8080:8080 spring-docker-demo:0.0.1

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

2021-12-23 00:39:47.873  INFO 1 --- [           main] com.example.Application                  : Starting Application v1.0-SNAPSHOT using Java 11.0.13 on a437d718d310 with PID 1 (/app/app.jar started by root in /)
2021-12-23 00:39:47.880  INFO 1 --- [           main] com.example.Application                  : No active profile set, falling back to default profiles: default
2021-12-23 00:39:49.961  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-12-23 00:39:49.985  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-12-23 00:39:49.985  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-12-23 00:39:50.088  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-12-23 00:39:50.088  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2029 ms
2021-12-23 00:39:50.444  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-12-23 00:39:50.867  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-12-23 00:39:50.900  INFO 1 --- [           main] com.example.Application                  : Started Application in 3.963 seconds (JVM running for 4.609)

有关run命令的详细信息,请查阅此处的参考资料。

image.png

使用 Cloud Native Buildpacks 进行容器化。

接下来,我们将使用Cloud Native Buildpacks进行容器化处理。

由于Spring Boot Maven插件与Cloud Native Buildpacks具有集成功能,因此可以使用以下命令创建容器镜像。

mvn spring-boot:build-image -Dspring-boot.build-image.imageName=[イメージ名]:[タグ]
mvn spring-boot:build-image -Dspring-boot.build-image.imageName=spring-docker-cnb-demo:0.0.1


[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.example:spring-docker-demo >-------------------
[INFO] Building spring-docker-demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.4.1:build-image (default-cli) > package @ spring-docker-demo >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ spring-docker-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] The encoding used to copy filtered properties files have not been set. This means that the same encoding will be used to copy filtered properties files as when copying other filtered resources. This might not be what you want! Run your build with --debug to see which files might be affected. Read more at https://maven.apache.org/plugins/maven-resources-plugin/examples/filtering-properties-files.html
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ spring-docker-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\sources\spring-docker-demo\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ spring-docker-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\sources\spring-docker-demo\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ spring-docker-demo ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ spring-docker-demo ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ spring-docker-demo ---
[INFO] Building jar: C:\sources\spring-docker-demo\target\app.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.4.1:repackage (repackage) @ spring-docker-demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] <<< spring-boot-maven-plugin:2.4.1:build-image (default-cli) < package @ spring-docker-demo <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.4.1:build-image (default-cli) @ spring-docker-demo ---
[INFO] Building image 'docker.io/library/spring-docker-cnb-demo:0.0.1'
[INFO] 
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 0%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 1%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 2%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 2%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 3%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 4%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 6%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 6%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 8%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 9%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 11%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 12%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 14%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 15%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 17%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 20%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 22%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 24%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 26%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 27%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 29%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 31%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 36%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 40%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 45%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 49%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 55%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 61%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 65%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 70%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 75%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 79%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 83%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 88%
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 100%
[INFO]  > Pulled builder image 'paketobuildpacks/builder@sha256:c212f4cde8f22e1517c4e919d64843c0d2287ad836b75449ae958f696183b458'
[INFO]  > Pulling run image 'docker.io/paketobuildpacks/run:base-cnb' 1%
[INFO]  > Pulling run image 'docker.io/paketobuildpacks/run:base-cnb' 47%
[INFO]  > Pulling run image 'docker.io/paketobuildpacks/run:base-cnb' 100%
[INFO]  > Pulled run image 'paketobuildpacks/run@sha256:50c74ebc95c169ea54ee4650d39d3c71d0fc738a3f0ad89b23d646cfd9ea36f2'
[INFO]  > Executing lifecycle version v0.13.2
[INFO]  > Using build cache volume 'pack-cache-c4b51991c14c.build'
[INFO] 
[INFO]  > Running creator
[INFO]     [creator]     ===> DETECTING
[INFO]     [creator]     6 of 19 buildpacks participating
[INFO]     [creator]     paketo-buildpacks/ca-certificates   3.0.1
[INFO]     [creator]     paketo-buildpacks/bellsoft-liberica 9.0.1
[INFO]     [creator]     paketo-buildpacks/syft              1.3.0
[INFO]     [creator]     paketo-buildpacks/executable-jar    6.0.1
[INFO]     [creator]     paketo-buildpacks/dist-zip          5.0.1
[INFO]     [creator]     paketo-buildpacks/spring-boot       5.2.0
[INFO]     [creator]     ===> ANALYZING
[INFO]     [creator]     Previous image with name "docker.io/library/spring-docker-cnb-demo:0.0.1" not found
[INFO]     [creator]     ===> RESTORING
[INFO]     [creator]     ===> BUILDING
[INFO]     [creator]     
[INFO]     [creator]     Paketo CA Certificates Buildpack 3.0.1
[INFO]     [creator]       https://github.com/paketo-buildpacks/ca-certificates
[INFO]     [creator]       Launch Helper: Contributing to layer
[INFO]     [creator]         Creating /layers/paketo-buildpacks_ca-certificates/helper/exec.d/ca-certificates-helper
[INFO]     [creator]     
[INFO]     [creator]     Paketo BellSoft Liberica Buildpack 9.0.1
[INFO]     [creator]       https://github.com/paketo-buildpacks/bellsoft-liberica
[INFO]     [creator]       Build Configuration:
[INFO]     [creator]         $BP_JVM_TYPE                 JRE             the JVM type - JDK or JRE
[INFO]     [creator]         $BP_JVM_VERSION              11.*            the Java version
[INFO]     [creator]       Launch Configuration:
[INFO]     [creator]         $BPL_DEBUG_ENABLED           false           enables Java remote debugging support
[INFO]     [creator]         $BPL_DEBUG_PORT              8000            configure the remote debugging port
[INFO]     [creator]         $BPL_DEBUG_SUSPEND           false           configure whether to suspend execution until a debugger has attached
[INFO]     [creator]         $BPL_HEAP_DUMP_PATH                          write heap dumps on error to this path
[INFO]     [creator]         $BPL_JAVA_NMT_ENABLED        true            enables Java Native Memory Tracking (NMT)
[INFO]     [creator]         $BPL_JAVA_NMT_LEVEL          summary         configure level of NMT, summary or detail
[INFO]     [creator]         $BPL_JFR_ARGS                                configure custom Java Flight Recording (JFR) arguments
[INFO]     [creator]         $BPL_JFR_ENABLED             false           enables Java Flight Recording (JFR)
[INFO]     [creator]         $BPL_JMX_ENABLED             false           enables Java Management Extensions (JMX)
[INFO]     [creator]         $BPL_JMX_PORT                5000            configure the JMX port
[INFO]     [creator]         $BPL_JVM_HEAD_ROOM           0               the headroom in memory calculation
[INFO]     [creator]         $BPL_JVM_LOADED_CLASS_COUNT  35% of classes  the number of loaded classes in memory calculation
[INFO]     [creator]         $BPL_JVM_THREAD_COUNT        250             the number of threads in memory calculation
[INFO]     [creator]         $JAVA_TOOL_OPTIONS                           the JVM launch flags
[INFO]     [creator]       BellSoft Liberica JRE 11.0.13: Contributing to layer
[INFO]     [creator]         Downloading from https://github.com/bell-sw/Liberica/releases/download/11.0.13+8/bellsoft-jre11.0.13+8-linux-amd64.tar.gz
[INFO]     [creator]         Verifying checksum
[INFO]     [creator]         Expanding to /layers/paketo-buildpacks_bellsoft-liberica/jre
[INFO]     [creator]         Adding 128 container CA certificates to JVM truststore
[INFO]     [creator]         Writing env.launch/BPI_APPLICATION_PATH.default
[INFO]     [creator]         Writing env.launch/BPI_JVM_CACERTS.default
[INFO]     [creator]         Writing env.launch/BPI_JVM_CLASS_COUNT.default
[INFO]     [creator]         Writing env.launch/BPI_JVM_SECURITY_PROVIDERS.default
[INFO]     [creator]         Writing env.launch/JAVA_HOME.default
[INFO]     [creator]         Writing env.launch/JAVA_TOOL_OPTIONS.append
[INFO]     [creator]         Writing env.launch/JAVA_TOOL_OPTIONS.delim
[INFO]     [creator]         Writing env.launch/MALLOC_ARENA_MAX.default
[INFO]     [creator]       Launch Helper: Contributing to layer
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/active-processor-count
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/java-opts
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/jvm-heap
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/link-local-dns
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/memory-calculator
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/openssl-certificate-loader
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/security-providers-configurer
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/jmx
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/jfr
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/nmt
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/security-providers-classpath-9
[INFO]     [creator]         Creating /layers/paketo-buildpacks_bellsoft-liberica/helper/exec.d/debug-9
[INFO]     [creator]       Java Security Properties: Contributing to layer
[INFO]     [creator]         Writing env.launch/JAVA_SECURITY_PROPERTIES.default
[INFO]     [creator]         Writing env.launch/JAVA_TOOL_OPTIONS.append
[INFO]     [creator]         Writing env.launch/JAVA_TOOL_OPTIONS.delim
[INFO]     [creator]     
[INFO]     [creator]     Paketo Syft Buildpack 1.3.0
[INFO]     [creator]       https://github.com/paketo-buildpacks/syft
[INFO]     [creator]         Downloading from https://github.com/anchore/syft/releases/download/v0.33.0/syft_0.33.0_linux_amd64.tar.gz
[INFO]     [creator]         Verifying checksum
[INFO]     [creator]         Writing env.build/SYFT_CHECK_FOR_APP_UPDATE.default
[INFO]     [creator]     
[INFO]     [creator]     Paketo Executable JAR Buildpack 6.0.1
[INFO]     [creator]       https://github.com/paketo-buildpacks/executable-jar
[INFO]     [creator]       Class Path: Contributing to layer
[INFO]     [creator]         Writing env/CLASSPATH.delim
[INFO]     [creator]         Writing env/CLASSPATH.prepend
[INFO]     [creator]       Process types:
[INFO]     [creator]         executable-jar: java org.springframework.boot.loader.JarLauncher (direct)
[INFO]     [creator]         task:           java org.springframework.boot.loader.JarLauncher (direct)
[INFO]     [creator]         web:            java org.springframework.boot.loader.JarLauncher (direct)
[INFO]     [creator]     
[INFO]     [creator]     Paketo Spring Boot Buildpack 5.2.0
[INFO]     [creator]       https://github.com/paketo-buildpacks/spring-boot
[INFO]     [creator]       Creating slices from layers index
[INFO]     [creator]         dependencies
[INFO]     [creator]         spring-boot-loader
[INFO]     [creator]         snapshot-dependencies
[INFO]     [creator]         application
[INFO]     [creator]       Launch Helper: Contributing to layer
[INFO]     [creator]         Creating /layers/paketo-buildpacks_spring-boot/helper/exec.d/spring-cloud-bindings
[INFO]     [creator]       Spring Cloud Bindings 1.8.0: Contributing to layer
[INFO]     [creator]         Downloading from https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.8.0/spring-cloud-bindings-1.8.0.jar
[INFO]     [creator]         Verifying checksum
[INFO]     [creator]         Copying to /layers/paketo-buildpacks_spring-boot/spring-cloud-bindings
[INFO]     [creator]       Web Application Type: Contributing to layer
[INFO]     [creator]         Servlet web application detected
[INFO]     [creator]         Writing env.launch/BPL_JVM_THREAD_COUNT.default
[INFO]     [creator]       4 application slices
[INFO]     [creator]       Image labels:
[INFO]     [creator]         org.opencontainers.image.title
[INFO]     [creator]         org.opencontainers.image.version
[INFO]     [creator]         org.springframework.boot.version
[INFO]     [creator]     ===> EXPORTING
[INFO]     [creator]     Adding layer 'paketo-buildpacks/ca-certificates:helper'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/bellsoft-liberica:helper'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/bellsoft-liberica:java-security-properties'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/bellsoft-liberica:jre'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/executable-jar:classpath'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/spring-boot:helper'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/spring-boot:spring-cloud-bindings'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/spring-boot:web-application-type'
[INFO]     [creator]     Adding 5/5 app layer(s)
[INFO]     [creator]     Adding layer 'launcher'
[INFO]     [creator]     Adding layer 'config'
[INFO]     [creator]     Adding layer 'process-types'
[INFO]     [creator]     Adding label 'io.buildpacks.lifecycle.metadata'
[INFO]     [creator]     Adding label 'io.buildpacks.build.metadata'
[INFO]     [creator]     Adding label 'io.buildpacks.project.metadata'
[INFO]     [creator]     Adding label 'org.opencontainers.image.title'
[INFO]     [creator]     Adding label 'org.opencontainers.image.version'
[INFO]     [creator]     Adding label 'org.springframework.boot.version'
[INFO]     [creator]     Setting default process type 'web'
[INFO]     [creator]     Saving docker.io/library/spring-docker-cnb-demo:0.0.1...
[INFO]     [creator]     *** Images (3615490a772e):
[INFO]     [creator]           docker.io/library/spring-docker-cnb-demo:0.0.1
[INFO]     [creator]     Adding cache layer 'paketo-buildpacks/syft:syft'
[INFO] 
[INFO] Successfully built image 'docker.io/library/spring-docker-cnb-demo:0.0.1'
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:12 min
[INFO] Finished at: 2021-12-23T10:03:59+09:00
[INFO] ------------------------------------------------------------------------

已经执行了创建JAR文件、下载所需的镜像和构建Docker镜像的操作。

检查容器镜像。

docker image ls

REPOSITORY                 TAG        IMAGE ID       CREATED        SIZE
spring-docker-demo         0.0.1      1f356d2363a4   2 hours ago    251MB
paketobuildpacks/run       base-cnb   a6fc5afbe7bb   7 days ago     87.2MB
paketobuildpacks/builder   base       73430d4688f2   42 years ago   760MB
spring-docker-cnb-demo     0.0.1      3615490a772e   42 years ago   262MB

我已经确认在构建时下载了镜像并创建了 Docker 镜像。

启动

docker run -it -p 8080:8080 spring-docker-cnb-demo:0.0.1
image.png

我已经成功启动了。

最终

我們這次使用了兩種方法進行容器化,但使用Cloud Native Buildpacks的方法更簡單。
使用Dockerfile的方法是一種簡易的記載,並沒有考慮到安全性和性能,在將其應用於生產環境時需要更詳細的記載,這種差異會更加顯著。

Cloud Native Buildpacks非常适合创建不易受到漏洞攻击的镜像状态,而无需编写Dockerfile,因此使用Cloud Native Buildpacks是最佳选择。

然而,由于所有操作都是自动执行的,无法进行详细的配置,因此我认为需要进行充分的验证,以确定是否可以在我的项目中无问题地使用。因此,我希望能继续进行调查和验证。

广告
将在 10 秒后关闭
bannerAds