3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

記事投稿キャンペーン 「2024年!初アウトプットをしよう」

Poetryのかゆいところを解決! コンテナ内のroot以外のユーザーでPoetryを動かす方法

Last updated at Posted at 2024-01-26

はじめに

コンテナ内のroot以外のユーザーで、poetry installをするときに権限の問題が困ることはありませんか?

今回はコンテナ内のroot以外のユーザーで、poetry installを実行する方法を紹介します。

内容

1.Poetry自体のインストール

まずDockerfileでcurlgccsudopoetryをインストールします。

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がおすすめです。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?