构建Docker 容器
很高兴的告诉你,在Spring Boot 2.3.x 版本开始, maven plugin将支持将应用程序打包成Docker镜像,无需额外的Dockerfile,只需要一条命令即可,不需要任何改动
mvn spring-boot:build-image
构建日志
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ k8s-demo-app ---
[INFO] Building jar: C:\Users\liangyong\workspace\new-demo\k8s-demo-app\target\k8s-demo-app-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.3.0.M4:repackage (repackage) @ k8s-demo-app ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.3.0.M4:build-image (default-cli) < package @ k8s-demo-app <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.3.0.M4:build-image (default-cli) @ k8s-demo-app ---
[INFO] Building image 'docker.io/library/k8s-demo-app:0.0.1-SNAPSHOT'
[INFO]
[INFO] > Pulling builder image 'docker.io/cloudfoundry/cnb:bionic-platform-api-0.2' 100%
[INFO] > Pulled builder image 'cloudfoundry/cnb@sha256:a601451579d595760d2678ea030ae938103c4b289e80f377b656a17e65855c59'
[INFO] > Pulling run image 'docker.io/cloudfoundry/run:base-cnb' 100%
[INFO] > Pulled run image 'cloudfoundry/run@sha256:bfe49e7d1c2c47d980af9dd684047616db872a982dcb2c5515a960d1a962a599'
[INFO] > Executing lifecycle version v0.6.1
[INFO] > Using build cache volume 'pack-cache-3231d9bc1a6c.build'
[INFO]
[INFO] > Running detector
[INFO] [detector] 6 of 13 buildpacks participating
[INFO] [detector] org.cloudfoundry.openjdk v1.2.14
[INFO] [detector] org.cloudfoundry.jvmapplication v1.1.12
[INFO] [detector] org.cloudfoundry.tomcat v1.3.18
[INFO] [detector] org.cloudfoundry.springboot v1.2.13
[INFO] [detector] org.cloudfoundry.distzip v1.1.12
[INFO] [detector] org.cloudfoundry.springautoreconfiguration v1.1.11
[INFO]
[INFO] > Running analyzer
[INFO] [analyzer] Warning: Image "docker.io/library/k8s-demo-app:0.0.1-SNAPSHOT" not found
[INFO]
[INFO] > Running restorer
[INFO]
[INFO] > Running builder
[INFO] [builder]
[INFO] [builder] Cloud Foundry OpenJDK Buildpack v1.2.14
[INFO] [builder] OpenJDK JRE 8.0.242: Contributing to layer
[INFO] [builder] Downloading from https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u242-b08/OpenJDK8U-jre_x64_linux_hotspot_8u242b08.tar.gz
[INFO] [builder] Verifying checksum
[INFO] [builder] Expanding to /layers/org.cloudfoundry.openjdk/openjdk-jre
[INFO] [builder] Writing JAVA_HOME to shared
[INFO] [builder] Writing MALLOC_ARENA_MAX to shared
[INFO] [builder] Writing .profile.d/active-processor-count
[INFO] [builder] Java Security Properties v1.2.14: Contributing to layer
[INFO] [builder] Writing JAVA_OPTS to launch
[INFO] [builder] Security Provider Configurer v1.2.14: Contributing to layer
[INFO] [builder] Writing .profile.d/security-provider-classpath
[INFO] [builder] Writing .profile.d/security-provider-configurer
[INFO] [builder] Link-Local DNS v1.2.14: Contributing to layer
[INFO] [builder] Writing .profile.d/link-local-dns
[INFO] [builder] JVMKill Agent 1.16.0: Contributing to layer
[INFO] [builder] Downloading from https://java-buildpack.cloudfoundry.org/jvmkill/bionic/x86_64/jvmkill-1.16.0-RELEASE.so
[INFO] [builder] Verifying checksum
[INFO] [builder] Copying to /layers/org.cloudfoundry.openjdk/jvmkill
[INFO] [builder] Writing JAVA_OPTS to shared
[INFO] [builder] Class Counter v1.2.14: Contributing to layer
[INFO] [builder] Memory Calculator 4.0.0: Contributing to layer
[INFO] [builder] Downloading from https://java-buildpack.cloudfoundry.org/memory-calculator/bionic/x86_64/memory-calculator-4.0.0.tgz
[INFO] [builder] Verifying checksum
[INFO] [builder] Set $BPL_HEAD_ROOM to configure. Default 0
[INFO] [builder] Set $BPL_LOADED_CLASS_COUNT to configure. Default 35%!!(MISSING)o(MISSING)f classes
[INFO] [builder] Set $BPL_THREAD_COUNT to configure. Default 250
[INFO] [builder] Expanding to /layers/org.cloudfoundry.openjdk/memory-calculator
[INFO] [builder] Writing .profile.d/memory-calculator
[INFO] [builder]
[INFO] [builder] Cloud Foundry JVM Application Buildpack v1.1.12
[INFO] [builder] Executable JAR: Contributing to layer
[INFO] [builder] Writing CLASSPATH to shared
[INFO] [builder] Process types:
[INFO] [builder] executable-jar: java -cp $CLASSPATH $JAVA_OPTS org.springframework.boot.loader.JarLauncher
[INFO] [builder] task: java -cp $CLASSPATH $JAVA_OPTS org.springframework.boot.loader.JarLauncher
[INFO] [builder] web: java -cp $CLASSPATH $JAVA_OPTS org.springframework.boot.loader.JarLauncher
[INFO] [builder]
[INFO] [builder] Cloud Foundry Spring Boot Buildpack v1.2.13
[INFO] [builder] Spring Boot 2.3.0.M4: Contributing to layer
[INFO] [builder] Writing CLASSPATH to shared
[INFO] [builder] 5 application slices
[INFO] [builder] Process types:
[INFO] [builder] spring-boot: java -cp $CLASSPATH $JAVA_OPTS com.example.k8sdemoapp.K8sDemoAppApplication
[INFO] [builder] task: java -cp $CLASSPATH $JAVA_OPTS com.example.k8sdemoapp.K8sDemoAppApplication
[INFO] [builder] web: java -cp $CLASSPATH $JAVA_OPTS com.example.k8sdemoapp.K8sDemoAppApplication
[INFO] [builder]
[INFO] [builder] Cloud Foundry Spring Auto-reconfiguration Buildpack v1.1.11
[INFO] [builder] Spring Auto-reconfiguration 2.11.0: Contributing to layer
[INFO] [builder] Downloading from https://repo.spring.io/release/org/cloudfoundry/java-buildpack-auto-reconfiguration/2.11.0.RELEASE/java-buildpack-auto-reconfiguration-2.11.0.RELEASE.jar
[INFO] [builder] Verifying checksum
[INFO] [builder] Copying to /layers/org.cloudfoundry.springautoreconfiguration/auto-reconfiguration
[INFO] [builder] Writing CLASSPATH to launch
[INFO]
[INFO] > Running exporter
[INFO] [exporter] Adding layer 'launcher'
[INFO] [exporter] Adding layer 'org.cloudfoundry.openjdk:class-counter'
[INFO] [exporter] Adding layer 'org.cloudfoundry.openjdk:java-security-properties'
[INFO] [exporter] Adding layer 'org.cloudfoundry.openjdk:jvmkill'
[INFO] [exporter] Adding layer 'org.cloudfoundry.openjdk:link-local-dns'
[INFO] [exporter] Adding layer 'org.cloudfoundry.openjdk:memory-calculator'
[INFO] [exporter] Adding layer 'org.cloudfoundry.openjdk:openjdk-jre'
[INFO] [exporter] Adding layer 'org.cloudfoundry.openjdk:security-provider-configurer'
[INFO] [exporter] Adding layer 'org.cloudfoundry.jvmapplication:executable-jar'
[INFO] [exporter] Adding layer 'org.cloudfoundry.springboot:spring-boot'
[INFO] [exporter] Adding layer 'org.cloudfoundry.springautoreconfiguration:auto-reconfiguration'
[INFO] [exporter] Adding 6/6 app layer(s)
[INFO] [exporter] Adding layer 'config'
[INFO] [exporter] *** Images (c7f56cf022f7):
[INFO] [exporter] docker.io/library/k8s-demo-app:0.0.1-SNAPSHOT
[INFO] [exporter] Reusing cache layer 'org.cloudfoundry.jvmapplication:executable-jar'
[INFO] [exporter] Reusing cache layer 'org.cloudfoundry.springboot:spring-boot'
[INFO]
[INFO] Successfully built image 'docker.io/library/k8s-demo-app:0.0.1-SNAPSHOT'
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:25 min
[INFO] Finished at: 2020-04-18T16:55:10+08:00
[INFO] ------------------------------------------------------------------------
查看构建的镜像
root@yong:~# docker images | grep k8s-demo-app
k8s-demo-app 0.0.1-SNAPSHOT c7f56cf022f7 40 years ago 224MB
输出的创建时间有点奇怪,不过是个小bug,不影响使用。 快去试试吧
更多的详细信息参考官方文档