はじめに
コンテナ内のroot以外のユーザーで、poetry install
をするときに権限の問題が困ることはありませんか?
今回はコンテナ内のroot以外のユーザーで、poetry install
を実行する方法を紹介します。
内容
1.Poetry自体のインストール
まずDockerfileでcurl
、gcc
、sudo
、poetry
をインストールします。
FROM python:3.9-buster
USER root
RUN apt-get -yq update && \
apt-get -yqq install curl gcc sudo && \
curl -sSL https://install.python-poetry.org | python3 -
poetry
自体のインストールはルートユーザーで行う必要があります。
poetry
は/root/.local/bin
に保存されます。
2.Poetryを動かす
devcontainerでコンテナを立ち上げた後、root以外のユーザー(例vscode
)でpoetry
を動かします。
poetry
は先述のように/root/.local/bin
にあります。以下のように絶対パスを指定しても実行できません。(もちろんパスを通しても同じです)
$ /root/.local/bin/poetry install
bash: /root/.local/bin/poetry: Permission denied
そこでsudo
権限を使えばpoetry
のコマンドの実行ができます。
$ sudo /root/.local/bin/poetry install
ただ毎回sudo /root/.local/bin/poetry
と打つのは面倒です。
そこで、alias poetry="sudo /root/.local/bin/poetry"
とすれば、以下のようにpoetry install
が通ります。
$ alias poetry="sudo /root/.local/bin/poetry"
$ poetry install
参考になれば幸いです。(そこそこ力業なので、最適なソリューションではないかもしれませんがご了承ください。)
追記:
PoetryではなくRyeを使えば、上記のような問題は発生しないので、筆者的にはRyeがおすすめです。