复制目标 jar 时,在 heroku 上复制失败



如何更改下面的 Dockerfile,以便它从正确的文件夹中复制 jar?!

我有这个 Dockerfile

# Docker multi-stage build
# 1. Building the App with Maven
#FROM maven:3-jdk-11
#FROM openjdk:8-jdk-alpine
FROM maven:3.5.2-jdk-8
# Just echo so we can see, if everything is there :)
RUN ls -l
RUN mvn install
RUN mvn jar:jar
VOLUME /tmp
RUN ls -l
RUN ls -l target/
# Add Spring Boot app.jar to Container
COPY "target/programmingpdfconv-0.0.1-SNAPSHOT.jar" app.jar
RUN ls /*
# Fire up our Spring Boot app by default
CMD [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

使用 Heroku 构建失败时本地正常:

remote: drwxr-xr-x 4 root root       64 Jun  7 16:47 classes
remote: drwxr-xr-x 3 root root       25 Jun  7 16:47 generated-sources
remote: drwxr-xr-x 3 root root       30 Jun  7 16:47 generated-test-sources
remote: drwxr-xr-x 2 root root       28 Jun  7 16:47 maven-archiver
remote: drwxr-xr-x 3 root root       35 Jun  7 16:47 maven-status
remote: -rw-r--r-- 1 root root 19578555 Jun  7 16:47 programmingpdfconv-0.0.1-SNAPSHOT.jar
remote: -rw-r--r-- 1 root root     5664 Jun  7 16:47 programmingpdfconv-0.0.1-SNAPSHOT.jar.original
remote: drwxr-xr-x 2 root root      163 Jun  7 16:47 surefire-reports
remote: drwxr-xr-x 3 root root       17 Jun  7 16:47 test-classes
remote: Removing intermediate container 9b42499c36c9
remote:  ---> 7f7adf538801
remote: Step 10/12 : COPY "target/programmingpdfconv-0.0.1-SNAPSHOT.jar" app.jar
remote: COPY failed: stat /var/lib/docker/tmp/docker-builder05459395/target/programmingpdfconv-0.0.1-SNAPSHOT.jar: no such file or directory
remote: 
remote: Verifying deploy...
remote: 
remote: !   Push rejected to pure-fortress-92268.
remote: 
To https://git.heroku.com/pure-fortress-92268.git
! [remote rejected] master -> master (pre-receive hook declined)

所以我看到要从中复制的文件夹在 heroku 上是不同的,我如何更改 Dockerfile 以便它从正确的文件夹中复制它?!(我尝试了很多方法都没有奏效。

Heroku.yml

build:
docker:
web: Dockerfile

添加项目的树结构:

$ tree
.
|-- Dockerfile
|-- Dockerfile.org
|-- Dockerfile.server
|-- README.md
|-- heroku.yml
|-- mvnw
|-- mvnw.cmd
|-- pom.xml
|-- src
|   |-- main
|   |   |-- java
|   |   |   `-- com
|   |   |       `-- tomdog
|   |   |           `-- programmingpdfconv
|   |   |               |-- GreetingController.java
|   |   |               `-- ProgrammingpdfconvApplication.java
|   |   `-- resources
|   |       |-- application.properties
|   |       `-- templates
|   |           |-- greeting.html
|   |           `-- index.html
|   `-- test
|       `-- java
|           `-- com
|               `-- tomdog
|                   `-- programmingpdfconv
|                       `-- ProgrammingpdfconvApplicationTests.java
`-- target
|-- classes
|   |-- application.properties
|   |-- com
|   |   `-- tomdog
|   |       `-- programmingpdfconv
|   |           |-- GreetingController.class
|   |           `-- ProgrammingpdfconvApplication.class
|   `-- templates
|       |-- greeting.html
|       `-- index.html
|-- generated-sources
|   `-- annotations
|-- generated-test-sources
|   `-- test-annotations
|-- maven-archiver
|   `-- pom.properties
|-- maven-status
|   `-- maven-compiler-plugin
|       |-- compile
|       |   `-- default-compile
|       |       |-- createdFiles.lst
|       |       `-- inputFiles.lst
|       `-- testCompile
|           `-- default-testCompile
|               |-- createdFiles.lst
|               `-- inputFiles.lst
|-- programmingpdfconv-0.0.1-SNAPSHOT.jar
|-- programmingpdfconv-0.0.1-SNAPSHOT.jar.original
|-- surefire-reports
|   |-- TEST-com.tomdog.programmingpdfconv.ProgrammingpdfconvApplicationTests.xml
|   `-- com.tomdog.programmingpdfconv.ProgrammingpdfconvApplicationTests.txt
`-- test-classes
`-- com
`-- tomdog
`-- programmingpdfconv
`-- ProgrammingpdfconvApplicationTests.class
36 directories, 31 files

使用COPY指令,<src>路径必须位于构建的上下文中,而不是在构建容器内。不能使用COPY复制容器内的数据。它在本地工作,因为您已经拥有以前版本中的target目录。尝试删除target目录并在本地生成映像,这次应该会失败。

解决方案是简单地将您COPY指令替换为

RUN cp -a target/programmingpdfconv-0.0.1-SNAPSHOT.jar app.jar

或者,如果您一步完成并清理,则可以显着减小图像大小

# not sure why you need to `install` the package in the local repository?
RUN mvn install jar:jar 
&& cp -a target/programmingpdfconv-0.0.1-SNAPSHOT.jar app.jar 
&& rm -rf target "$HOME/.m2"

您还应考虑从 docker 构建上下文中排除 maven 构建目录target,以通过将.dockerignore文件添加到根目录来提高构建性能。

最新更新