【问题标题】:docker-compose build download few pom dependencies each timedocker-compose build 每次下载几个 pom 依赖
【发布时间】:2017-07-12 16:27:18
【问题描述】:

我正在使用 Heroku Java Docker Imagedocker-compose 在本地运行基于 dropwizard java 的 Web 服务。

当我运行docker-compose build web 命令构建代码时,它每次都会下载一些依赖项。因此构建过程的周转时间增加了。

我的项目 docker 文件只有一行: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 没有为上述库使用缓存的依赖项。如何强制使用缓存的依赖?

谷歌了很多,但没有运气。如果有人遇到并修复过,请分享。

更新:

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

【问题讨论】:

  • 问题是什么?
  • 如果你想使用你已经构建的镜像,你可以使用 up 命令代替 build。但是,如果您在 Dockerfile 中有 ADD 或 COPY 并且修改了文件,则必须添加 --build 标志来重建映像。
  • @AndyShinn 更新问题
  • 能否提供docker-compose.yml 和足够的服务和文件(Dockerfile 等)以便我们重现?
  • @FrançoisNoël 我在我的 Dockerfile 中使用heroku/java,并且每次更改代码时都在构建。

标签: maven heroku docker-compose dockerfile dropwizard


【解决方案1】:

我之前的回答没有意义(我错误地认为 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> 就可以逃脱)。

【讨论】:

  • 出现错误:[ERROR] Malformed POM /app/user/pom.xml: Unrecognised tag: 'scope' ,我认为 没有 标签。
  • 我尝试了几种方法,但没有找到任何解决方案。如果有其他方法,请给我建议。
  • 我没有其他想法。
猜你喜欢
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
  • 2011-09-19
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多