[英]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: 基于在本地计算机上按以下步骤尝试的注释:
ran command mvn clean
. 运行命令
mvn clean
。 That cleaned target folder 清理目标文件夹
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”]
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
命令和创建的映像。
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: 补充笔记:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.