簡體   English   中英

pip 安裝在運行 docker 容器內

[英]pip install inside running docker container

我有以下Dockerfile

FROM python:3.9.1

ARG APP_USER=anychat
RUN groupadd -r ${APP_USER} && useradd --no-log-init -r -g ${APP_USER} ${APP_USER}

WORKDIR /app/
COPY requirements.txt /app/

RUN set -ex \
    && BUILD_DEPS=" \
    build-essential \
    libpcre3-dev \
    libpq-dev \
    vim \
    " \
    && apt-get update && apt-get install -y --no-install-recommends $BUILD_DEPS \
    && pip install -r requirements.txt

COPY ./src /app/

EXPOSE 8000

ENTRYPOINT ["/app/scripts/docker/entrypoint.sh"]

運行容器后,我想在運行容器內安裝 python 庫

$ docker exec -it 1ab2a4b34 bash
anychat@947756b6ae96:/app pip install requests

但這給出了錯誤

WARNING: The directory '/home/anychat/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Defaulting to user installation because normal site-packages is not writeable
Collecting requests
  Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB)
     |████████████████████████████████| 61 kB 3.0 MB/s 
Collecting certifi>=2017.4.17
  Downloading certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
     |████████████████████████████████| 147 kB 3.3 MB/s 
Collecting chardet<5,>=3.0.2
  Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB)
     |████████████████████████████████| 178 kB 3.3 MB/s 
Collecting idna<3,>=2.5
  Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
     |████████████████████████████████| 58 kB 4.7 MB/s 
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.2-py2.py3-none-any.whl (136 kB)
     |████████████████████████████████| 136 kB 1.8 MB/s 
Installing collected packages: urllib3, idna, chardet, certifi, requests
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/home/anychat/.local'
Check the permissions.

問題是您正在嘗試安裝軟件包,但是:

  1. 您不是root ,因此pip無法寫入 systemd 范圍的位置,並且
  2. 您的anychat用戶沒有主目錄,因此pip無法寫入默認用戶位置。

有幾種方法可以解決這個問題。 最簡單的方法可能是確保您的anychat用戶有一個主目錄。 而不是寫:

useradd --no-log-init -r -g ${APP_USER} ${APP_USER}

利用:

useradd --no-log-init -r -m -g ${APP_USER} ${APP_USER}

-m標志要求useradd創建相應的主目錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM