在 Docker 容器 Django 中使用 Pip 安装包时权限被拒绝

Permission Denied When Installing Packages Using Pip In Docker Container Django

我无法在我的 docker 容器中安装包,请告诉我如何解决这个问题。

警告:

WARNING: The directory '/home/app/.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 should use sudo's -H flag.

错误:

ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '/home/app'
Check the permissions.

Docker 文件:

FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /app
EXPOSE 8000

COPY ./core/ /app/
COPY ./scripts /scripts

RUN pip install --upgrade pip
COPY requirements.txt /app/
RUN pip install -r requirements.txt && \
    adduser --disabled-password --no-create-home app && \
    mkdir -p /vol/web/static && \
    mkdir -p /vol/web/media && \
    chown -R app:app /vol && \
    chmod -R 755 /vol && \
    chmod -R +x /scripts

USER app

CMD ["/scripts/run.sh"]

容器内命令:

pip install django-storages

在我的例子中,我通过添加 -u 0 使用 root 用户安装了包,这告诉 docker 以 root 用户身份进入。

docker exec -u 0 -it mycontainer bash