我正在使用Heroku Java Docker Image和docker-compose在本地运行基于dropwizard java的Web服务。
当我运行docker-compose build web
命令来构建代码时,每次都会下载一些依赖项。因此构建流程周转时间增加了。
我的项目泊坞窗文件只有一行:FROM heroku/java
这是构建日志:
[INFO] ------------------------------------------------------------------------
[INFO] Building generator-app-server 0.0.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom (4 KB at 0.7 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/22/maven-plugins-22.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/22/maven-plugins-22.pom (13 KB at 9.0 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.jar (25 KB at 14.7 KB/sec)
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ generator-app-server ---
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.pom (4 KB at 5.4 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/sonatype/spice/spice-parent/16/spice-parent-16.pom
Downloaded: https://repo.maven.apache.org/maven2/org/sonatype/spice/spice-parent/16/spice-parent-16.pom (9 KB at 4.9 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/sonatype/forge/forge-parent/5/forge-parent-5.pom
Downloaded: https://repo.maven.apache.org/maven2/org/sonatype/forge/forge-parent/5/forge-parent-5.pom (9 KB at 5.0 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.jar
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.jar (221 KB at 20.0 KB/sec)
[INFO] Deleting /app/user/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ generator-app-server ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 7 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ generator-app-server ---
Downloading: https://repo.maven.apache.org/maven2/org/mapstruct/mapstruct-processor/1.1.0.Final/mapstruct-processor-1.1.0.Final.jar
Downloaded: https://repo.maven.apache.org/maven2/org/mapstruct/mapstruct-processor/1.1.0.Final/mapstruct-processor-1.1.0.Final.jar (1502 KB at 24.5 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/mapstruct/mapstruct-processor/1.1.0.Final/mapstruct-processor-1.1.0.Final.pom
Downloaded: https://repo.maven.apache.org/maven2/org/mapstruct/mapstruct-processor/1.1.0.Final/mapstruct-processor-1.1.0.Final.pom (12 KB at 4.9 KB/sec)
docker-compose build
没有为上面的lib使用缓存依赖项。如何强制使用缓存的依赖项?
更新
Dockerfile
FROM heroku/java
docker-compose.yml
web:
build: .
command: 'bash -c ''java $JAVA_OPTS -jar target/generator-app-server-0.0.2-SNAPSHOT.jar db migrate config.yml && java $JAVA_OPTS -Ddw.server.connector.port=$PORT -jar target/generator-app-server-0.0.2-SNAPSHOT.jar server config.yml'''
working_dir: /app/user
environment:
PORT: 8080
DATABASE_URL: 'postgres://postgres:@herokuPostgresql:5432/postgres'
ports:
- '8080:8080'
links:
- herokuPostgresql
shell:
build: .
command: bash
working_dir: /app/user
environment:
PORT: 8080
DATABASE_URL: 'postgres://postgres:@herokuPostgresql:5432/postgres'
ports:
- '8080:8080'
links:
- herokuPostgresql
volumes:
- '.:/app/user'
herokuPostgresql:
image: postgres
答案 0 :(得分:0)
我以前的回答没有意义(我错误地认为Maven依赖项是在运行时处理的,而是在ONBUILD
指令处理它们)。但我会尝试再次解释为什么不缓存这些特定的依赖关系。
问题中列出的下载依赖项来自Maven clean
的内置插件。在父Dockerfile
中,我们在https://github.com/heroku/docker-java/blob/master/Dockerfile#L14(POM中的所有内容)下载项目依赖项。然后,稍后在Dockerfile
中,它会在https://github.com/heroku/docker-java/blob/master/Dockerfile#L18处运行clean
。由于maven-clean-plugin
不属于您的POM,因此会在清洁步骤中自动下载(不会从之前的Dockerfile
指令中缓存)。
所以,如果你想缓存maven-clean-plugin
,你可能需要将它作为POM中的依赖项添加(你可能只能使用<scope>import</scope>
)。