繁体   English   中英

Google Cloud Platform管道/容器构建器在Spring Boot Java Application中使用COPY或ADD命令构建docker映像时出现问题

[英]Google Cloud Platform pipeline/container builder issue building docker image using COPY or ADD command for Spring Boot Java Application

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

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

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

Dockerfile如下所示

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

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

docker build . -t helloworld:v1

通过创建容器进行验证。 将代码签入到docker-hub帐户和github帐户。

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

当构建被选择运行时,在Dockerfile中此行失败

ADD target/helloworld.jar helloworld.jar

在GCP日志中看到的错误:

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

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

注意:我尝试使用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}'
      - '.'

这没有任何区别。 问题保持不变。

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


更新-1

基于在本地计算机上按以下步骤尝试的注释:

  1. 运行命令mvn clean 清理目标文件夹

  2. 更新的Dockerfile

从maven:3.5-jdk-8 AS构建
复制src。
复制pom.xml。
运行mvn -f pom.xml清洁程序包

来自openjdk:8
音量/ tmp
复制--from = build target / helloworld.jar helloworld.jar
展8081
ENTRYPOINT [“ java”,“-jar”,“ helloworld.jar”]

  1. docker build . -t helloworld:v1 docker build . -t helloworld:v1命令和创建的映像。

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

容器启动和退出,并在日志中显示错误:

Exception in thread "main" java.lang.ClassNotFoundException: com.example.HelloWorldApplication at java.net.URLClassLoader.findClass(URLClassLoader.java:382)

看起来像文件路径有问题。

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

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"]

补充笔记:

  • 请参阅相关答案以获取构建Spring Boot应用程序的完整示例
  • 我已经将第二阶段基于JRE映像。 缩小输出图像的尺寸。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM