如何更改下面的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
答案 0 :(得分:1)
使用COPY
指令,<src>
路径必须在构建上下文内,而不是构建容器内;您不能使用它在容器内复制数据。它在本地工作,因为您已经拥有先前构建的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
文件来提高构建性能。