简体   繁体   English

pip 安装在运行 docker 容器内

[英]pip install inside running docker container

I have the following Dockerfile我有以下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"]

After running the container, I want to install the python library inside the running container运行容器后,我想在运行容器内安装 python 库

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

But this gives the error但这给出了错误

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.

The problem is that you are trying to install packages, but:问题是您正在尝试安装软件包,但是:

  1. You are not root , so pip can't write to the systemd-wide locations, and您不是root ,因此pip无法写入 systemd 范围的位置,并且
  2. Your anychat user has no home directory, so pip can't write to the default user location.您的anychat用户没有主目录,因此pip无法写入默认用户位置。

There are a few ways of addressing this problem.有几种方法可以解决这个问题。 The easiest is probably to ensure that your anychat user has a home directory.最简单的方法可能是确保您的anychat用户有一个主目录。 Instead of writing:而不是写:

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

Use:利用:

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

The -m flag asks useradd to create the corresponding home directory. -m标志要求useradd创建相应的主目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM