简体   繁体   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

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

pom.xml has maven plugin entry like below pom.xml具有如下所示的maven插件条目

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

Dockerfile looks like below Dockerfile如下所示

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

Verified by creating container out of it. 通过创建容器进行验证。 Checked in code to docker-hub account and github account. 将代码签入到docker-hub帐户和github帐户。

Logged into Google cloud platform (GCP), created kubernetes cluster, created pipeline(using container builder) by configuring github url where helloworld microservice code resides. 通过配置helloworld微服务代码所在的github url,登录到Google云平台(GCP),创建kubernetes集群,创建管道(使用容器生成器)。 There are two options to run build (use Dockerfile or cloudbuild.yaml). 有两个选项来运行构建(使用Dockerfile或cloudbuild.yaml)。 I am using Dockerfile to run build. 我正在使用Dockerfile运行构建。

When build is picked up to run, it fails for this line in Dockerfile 当构建被选择运行时,在Dockerfile中此行失败

ADD target/helloworld.jar helloworld.jar

Error seen in GCP logs: 在GCP日志中看到的错误:

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

I tried to replace it with COPY command and still the issue is same. 我试图用COPY命令替换它,但问题仍然相同。

Note: I tried to go with cloudbuild.yaml Here is how my cloudbuild.yaml looks: 注意:我尝试使用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. 问题保持不变。

Any idea if Springboot Java application has some specific configuration for Dockerfile to be built fine in Google Cloud Platform? 知道Springboot Java应用程序是否具有可以在Google Cloud Platform中正常构建的Dockerfile的特定配置吗?


UPDATE - 1 更新-1

Based on comments tried below steps on local machine: 基于在本地计算机上按以下步骤尝试的注释:

  1. ran command mvn clean . 运行命令mvn clean That cleaned target folder 清理目标文件夹

  2. updated Dockerfile 更新的Dockerfile

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

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

  1. Ran docker build . -t helloworld:v1 docker build . -t helloworld:v1 docker build . -t helloworld:v1 command and that created image. docker build . -t helloworld:v1命令和创建的映像。

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

container starts and exits with error in log: 容器启动和退出,并在日志中显示错误:

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

Looks like a problem with file paths. 看起来像文件路径有问题。

Try the following updated Dockerfile, which explicitly sets the working directory. 尝试以下更新的Dockerfile,该文件显式设置工作目录。 It also uses explicit file paths when copying the jar between images. 在映像之间复制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"]

Additional Notes: 补充笔记:

  • See the related answer for a full example building a spring boot app 请参阅相关答案以获取构建Spring Boot应用程序的完整示例
  • I've based the second stage on a JRE image. 我已经将第二阶段基于JRE映像。 Reduces the size of the output image. 缩小输出图像的尺寸。

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

相关问题 如何从 Linux 命令行或 docker 容器中检查 spring 启动 java 应用程序消耗了哪些值 - How to check what values are consumed by a spring boot java application from the Linux command line or in the docker container Google Cloud Platform - Cloud SQL 上 MySQL 的 Spring Boot 应用程序连接问题 - Google Cloud Platform - Spring Boot application connection issues with MySQL on Cloud SQL Java Docker 申请 - Issue with Building Java Application in Docker 从 java spring boot 应用程序调用谷歌云函数 - calling google cloud functions from java spring boot application 使用 java11 在谷歌云上使用 mysql 部署 Spring 启动应用程序 - Deploying a Spring boot application with mysql on google cloud with java11 无法让 Spring 引导应用程序在 Google Cloud Platform Flexible Environment 上工作 - Can't get Spring Boot application to work on Google Cloud Platform Flexible Environment 如果我们已在Google Cloud Platform中部署Spring Boot应用程序,如何查看日志? - How to see the logs if we have deployed our Spring Boot Application in Google Cloud Platform? 在docker容器中重新部署spring-boot应用程序? - Redeploy spring-boot application in docker container? 使用Spring Boot构建Spring MVC应用程序 - Building Spring MVC application using Spring Boot Angular2前端在Google云平台上的Spring Boot后端 - Angular2 frontend Spring Boot backend on Google cloud platform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM