yanom blog

様々な技術について書きます

docker上でgradleを動かす際の権限でハマった

docker上でgradleのJavaアプリビルドを実行する際に少しハマったので記事にしておきます。

エラー内容

$./gradlew build
Downloading https://services.gradle.org/distributions/gradle-5.1.1-bin.zip
.................................................................................

Welcome to Gradle 5.1.1!

Here are the highlights of this release:
 - Control which dependencies can be retrieved from which repositories
 - Production-ready configuration avoidance APIs

For more details see https://docs.gradle.org/5.1.1/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)

FAILURE: Build failed with an exception.

* What went wrong:
Could not create service of type ScriptPluginFactory using BuildScopeServices.createScriptPluginFactory().
> Could not create service of type FileHasher using BuildSessionScopeServices.createFileSnapshotter().

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 31s

--stacktraceを付与してビルドを行うと詳細なログが出力されます。

$ ./gradlew build --stacktrace
(中略)
Caused by: org.gradle.api.UncheckedIOException: java.io.IOException: Permission denied

ということで、コンテナ内へファイルをコピーする際に所有者がrootになってしまっており、 それが原因でPermission deniedが発生していることがわかりました。

解決方法

ADD --chown=gradle:gradle app /opt/app

とすることで、所有者をgradleに変更しつつコピー出来ます。