在Docker中构建gradle项目

时间:2017-10-17 14:21:13

标签: docker gradle build.gradle file-permissions

我有一个简单的gradle项目,我想在Docker容器中构建

我有一个多级docker构建。第一阶段只是从github克隆项目。第二阶段必须建立项目,以便我可以在最后阶段运行它。但它在

中失败了
FROM gradle:4.2.1-jdk8-alpine
WORKDIR /app
COPY --from=0 /app/myProject /app
RUN ./gradlew build --stacktrace

命令./gradlew build --stacktrace无法执行,但失败并显示错误:

FAILURE: Build failed with an exception.

* What went wrong:
Could not update /app/.gradle/3.5-rc-2/file-changes/last-build.bin

堆栈跟踪:

org.gradle.api.UncheckedIOException: Could not update /app/.gradle/3.5-rc-2/file-changes/last-build.bin
    at org.gradle.api.internal.changedetection.state.FileTimeStampInspector.updateOnFinishBuild(FileTimeStampInspector.java:72)
    at org.gradle.api.internal.changedetection.state.BuildScopeFileTimeStampInspector.stop(BuildScopeFileTimeStampInspector.java:38)
    at org.gradle.internal.concurrent.CompositeStoppable.stop(CompositeStoppable.java:98)
.....
Caused by: java.io.FileNotFoundException: /app/.gradle/3.5-rc-2/file-changes/last-build.bin (Permission denied)
    at java.io.FileOutputStream.open0(Native Method)
......

为什么docker用户没有权限在工作目录中创建文件以及如何添加这些权限? 简单的RUN chmod 777 /app不起作用,并告诉我:chmod: /app: Operation not permitted

2 个答案:

答案 0 :(得分:10)

根据Dockerfile gradle:4.2.1-jdk8-alpine at,它有" gradle"作为其默认用户。您从app目录复制到docker image的app目录的文件可能没有#34; gradle"用户。

您应该在Dockerfile中添加三个额外的命令来设置正确的权限:

FROM gradle:4.2.1-jdk8-alpine
WORKDIR /app
COPY --from=0 /app/myProject /app

USER root                # This changes default user to root
RUN chown -R gradle /app # This changes ownership of folder
USER gradle              # This changes the user back to the default user "gradle"

RUN ./gradlew build --stacktrace

答案 1 :(得分:2)

另一种选择是使用ADD命令而不是COPY,然后使用它的--chown选项在复制后更改文件的所有者。因此,最终的Dockerfile会更加简单。

FROM gradle:4.2.1-jdk8-alpine
WORKDIR /app
ADD --chown=gradle:gradle /app/myProject /app

RUN ./gradlew build --stacktrace