本文介绍了Google Cloud Platform管道/容器构建器在Spring Boot Java Application中使用COPY或ADD命令构建docker映像时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring Boot(2.1.3),Java 8,Maven创建基本的HelloWorld微服务。

Created basic HelloWorld microservice using Spring Boot (2.1.3), Java 8, Maven.

pom.xml具有如下所示的maven插件条目

pom.xml has maven plugin entry like below

<plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                    <mainClass>com.example.HelloWorldApplication</mainClass>
            </configuration>
        </plugin>

Dockerfile如下所示

Dockerfile looks like below

FROM openjdk:8
VOLUME /tmp
ADD target/helloworld.jar helloworld.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","helloworld.jar"]

在本地计算机上创建图像使用命令

Created image on local machine using command

docker build . -t helloworld:v1

已通过在其中创建容器来进行验证。
登录到docker-hub帐户和github帐户的代码。

Verified by creating container out of it.Checked in code to docker-hub account and github account.

登录到Google云平台(GCP),创建kubernetes集群,创建管道(使用容器生成器),配置helloworld微服务代码所驻留的github url。有两个选项来运行构建(使用Dockerfile或cloudbuild.yaml)。我正在使用Dockerfile运行构建。

Logged into Google cloud platform (GCP), created kubernetes cluster, created pipeline(using container builder) by configuring github url where helloworld microservice code resides. There are two options to run build (use Dockerfile or cloudbuild.yaml). I am using Dockerfile to run build.

选择构建运行时,Dockerfile中的这一行将失败

When build is picked up to run, it fails for this line in Dockerfile

ADD target/helloworld.jar helloworld.jar

在GCP日志中看到错误:

Error seen in GCP logs:

ADD failed: stat /var/lib/docker/tmp/docker-builderxxxxxx/target/helloworld.jar: no such file or directory

我尝试用COPY命令替换它,但问题仍然是相同。

I tried to replace it with COPY command and still the issue is same.

注意:我尝试使用cloudbuild.yaml
这是我的cloudbuild.yaml的外观:

  steps:
  # Build the helloworld container image.
  - name: 'gcr.io/cloud-builders/docker'
    args:
      - 'build'
      - '-t'
      - 'gcr.io/${PROJECT_ID}/helloworld:${TAG_NAME}'
      - '.'

这没有任何区别。问题仍然存在。

This didn't make any difference. Issue remains the same.

是否知道Springboot Java应用程序是否具有可以在Google Cloud Platform中正常构建的Dockerfile的特定配置?

Any idea if Springboot Java application has some specific configuration for Dockerfile to be built fine in Google Cloud Platform?

更新-1

基于下面在本地计算机上尝试执行的注释:

Based on comments tried below steps on local machine:


  1. 运行命令 mvn clean 。清理了 target 文件夹

更新后的Dockerfile

updated Dockerfile



FROM openjdk:8

VOLUME / tmp

COPY --from = build target / helloworld.jar helloworld.jar

EXPOSE 8081

ENTRYPOINT [ java,-jar, helloworld.jar]

FROM openjdk:8
VOLUME /tmp
COPY --from=build target/helloworld.jar helloworld.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","helloworld.jar"]




  1. Ran docker build。 -t helloworld:v1 命令并创建了映像。

然后运行命令以启动容器:
docker run -p 8081:8081 -n helloworld-app -d helloworld:v1

Then run command to start container: docker run -p 8081:8081 -n helloworld-app -d helloworld:v1

容器启动并退出,并在日志中出现错误:

container starts and exits with error in log:

线程主中的异常java.lang.ClassNotFoundException:com.example。 java.net.URLClassLoader.findClass(URLClassLoader.java:382)上的HelloWorldApplication

推荐答案

看起来像文件路径问题。

Looks like a problem with file paths.

尝试以下更新的Dockerfile,该文件明确设置了工作目录。在映像之间复制jar时,它也使用显式文件路径。

Try the following updated Dockerfile, which explicitly sets the working directory. It also uses explicit file paths when copying the jar between images.

FROM maven:3.5-jdk-8-slim AS build
WORKDIR /home/app
COPY src     /home/app/src
COPY pom.xml /home/app
RUN mvn clean package

FROM openjdk:8-jre-slim
COPY --from=build /home/app/target/helloworld-0.0.1-SNAPSHOT.jar /usr/local/lib/helloworld.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","/usr/local/lib/helloworld.jar"]

附加说明:


  • 请参见,其中包含构建弹簧启动应用的完整示例

  • 我基于JRE图像上的第二阶段。减小输出图像的大小。

  • See the related answer for a full example building a spring boot app
  • I've based the second stage on a JRE image. Reduces the size of the output image.

这篇关于Google Cloud Platform管道/容器构建器在Spring Boot Java Application中使用COPY或ADD命令构建docker映像时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 01:38