October 12, 2017

OpenShift にデプロイするとき権限でハマる

わけあって OpenShift Starter にアプリをデプロイしようとしていたのだけど、OpenShift は相変わらず独自仕様がてんこ盛り。v2 のときも独自仕様がたんまりあって 『Heroku なら楽勝で動くんだけどなー』みたいなことが多かったが v3 の今回も似た様な感じ。

この辺に色々書いてある。有名な話だが OpenShift でデプロイされるコンテナはセキュリティ確保のため root ユーザで動かない。動的にユーザ ID が割り当たるようになっている。(つまり docker run --user <ここがランダムやで> ... のようなイメージ)

root で動かす前提のコンテナはもちろん動かないし、Dockerfile で USER 指定しても無視される。 ついでに罠なのは sudo で、動的にアサインされる UID とか sudores 書けないし etc, etc, etc…

一応 oc (OpenShift CLI) コマンドを叩くことで事で Dockerfile の USER を使ってくれたり root を許可できるようだけど Starter で使えるか分からないし、そもそも oc コマンドなんて普通インストールされてない。

・・・と思いながらドキュメントを読んでいけば比較的簡単に答えにたどり着く。

Adding the following to your Dockerfile sets the directory and file permissions to allow users in the root group to access them in the built image:

RUN chgrp -R 0 /some/directory && \
   chmod -R g=u /some/directory

Because the container user is always a member of the root group, the container user can read and write these files. The root group does not have any special permissions (unlike the root user) so there are no security concerns with this arrangement.

と言うわけで Dockerfile を少し弄ることで私の課題は解決した。

とは言え、このあたりは結構根が深い問題で bitnami とか大分フラストレーション溜まってそうな雰囲気。

というか、そのままだとサンプルでよく使われる DockerHub 公式ビルドの nginx コンテナすら動かないので『エコシステムとは(真顔』となるのは否定できない。

© Kazuhisa Hara 2014-2022