Django - 以“root”用户身份运行 pip 可能会导致权限损坏

问题描述 投票:0回答:1

我正在利用 AWS 应用程序运行程序服务部署 Django 后端。我的文件内容如下。

apprunner.yaml

version: 1.0
runtime: python3
build:
  commands:
    build:
      - pip install -r requirements.txt 
run:
  runtime-version: 3.8.16
  command: sh startup.sh
  network:
    port: 8000

需求.txt

asttokens==2.2.1
backcall==0.2.0
category-encoders==2.6.0
certifi==2023.7.22
charset-normalizer==3.3.0
colorama>=0.2.5, <0.4.5
comm==0.1.3
contourpy==1.0.7
cycler==0.11.0
debugpy==1.6.7
decorator==5.1.1
distlib==0.3.7
executing==1.2.0
filelock==3.13.0
fonttools==4.40.0
gunicorn==20.1.0
idna==3.4
ipykernel==6.23.2
ipython>=7.0.0, <8.0.0
jedi==0.18.2
joblib==1.2.0
jupyter_client==8.2.0
jupyter_core==5.3.0
kiwisolver==1.4.4
matplotlib==3.7.1
matplotlib-inline==0.1.6
nest-asyncio==1.5.6
numpy==1.24.2
opencv-python==4.7.0.68
packaging==23.0
pandas==1.5.3
parso==0.8.3
patsy==0.5.3
pickleshare==0.7.5
Pillow==9.5.0
pipenv==2023.10.24
platformdirs==3.11.0
prompt-toolkit==3.0.38
psutil==5.9.5
pure-eval==0.2.2
pycodestyle==2.10.0
pygame==2.1.3
Pygments==2.15.1
pyparsing==3.0.9
python-dateutil==2.8.2
pytz==2022.7.1
pyzmq==25.1.0
scikit-learn==1.2.1
scipy==1.10.0
seaborn==0.12.2
six==1.16.0
stack-data==0.6.2
statsmodels==0.13.5
threadpoolctl==3.1.0
tornado==6.3.2
traitlets==5.9.0
urllib3>=1.25.4, <1.27
virtualenv==20.24.6
wcwidth==0.2.6
whitenoise==6.4.0

startup.sh

#!/bin/bash
python manage.py collectstatic && gunicorn --workers 2 backend.wsgi

顺便说一句,所有软件包都已成功安装在AWS应用程序运行器中,最后它给出了

10-29-2023 02:39:34 AM [Build] [91mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
10-29-2023 02:39:34 AM [Build] [0mRemoving intermediate container 4a0110123b9d
10-29-2023 02:39:34 AM [Build]  ---> fca9ca934529
10-29-2023 02:39:34 AM [Build] Step 5/5 : EXPOSE 8000
10-29-2023 02:39:34 AM [Build]  ---> Running in c8a4669398b0
10-29-2023 02:39:34 AM [Build] Removing intermediate container c8a4669398b0
10-29-2023 02:39:34 AM [Build]  ---> 1f0d784181f3
10-29-2023 02:39:34 AM [Build] Successfully built 1f0d784181f3
10-29-2023 02:39:34 AM [Build] Successfully tagged application-image:latest
10-29-2023 02:42:13 AM [AppRunner] Failed to deploy your application source code.

但是我更改了 apprunner.yaml 代码以将 virtualenv 包含为

version: 1.0
runtime: python3
build:
  commands:
    build:
      - pip install pipenv
      - pipenv install -r requirements.txt
run:
  runtime-version: 3.8.16
  command: sh startup.sh
  network:
    port: 8000

但是即使没有安装软件包,应用程序运行程序也会失败并给出错误:

10-29-2023 02:29:06 AM [Build] Warning: the environment variable LANG is not set!
10-29-2023 02:29:06 AM [Build] We recommend setting this in ~/.profile (or equivalent) for proper expected behavior.
10-29-2023 02:29:06 AM [Build] Warning: Python 3.11 was not found on your system...
10-29-2023 02:29:06 AM [Build] Creating a virtualenv for this project...
10-29-2023 02:29:06 AM [Build] Pipfile: /codebuild/output/src2729792062/src/backend/Pipfile
10-29-2023 02:29:06 AM [Build] Using default python from /root/.pyenv/versions/3.9.16/bin/python3.9 (3.9.16) to create virtualenv...
10-29-2023 02:29:06 AM [Build] created virtual environment CPython3.9.16.final.0-64 in 1333ms
10-29-2023 02:29:06 AM [Build]   creator CPython3Posix(dest=/root/.local/share/virtualenvs/backend-C4VHmbmy, clear=False, no_vcs_ignore=False, global=False)
10-29-2023 02:29:06 AM [Build]   seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
10-29-2023 02:29:06 AM [Build]     added seed packages: pip==23.2, setuptools==68.0.0, wheel==0.40.0
10-29-2023 02:29:06 AM [Build]   activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
10-29-2023 02:29:06 AM [Build] ✔ Successfully created virtual environment!
10-29-2023 02:29:06 AM [Build] Virtualenv location: /root/.local/share/virtualenvs/backend-C4VHmbmy
10-29-2023 02:29:06 AM [Build] Warning: Your Pipfile requires python_version 3.11, but you are using 3.9.16 (/root/.local/share/v/b/bin/python).
10-29-2023 02:29:06 AM [Build]   $ pipenv --rm and rebuilding the virtual environment may resolve the issue.
10-29-2023 02:29:06 AM [Build] Usage: pipenv install [OPTIONS] [PACKAGES]...
10-29-2023 02:29:06 AM [Build] ERROR:: Aborting deploy
10-29-2023 02:29:16 AM [AppRunner] Failed to deploy your application source code.

所以我将 apprunner.yaml 中的

runtime-version
更改为 3.11.0 然后 apprunner 给出:

10-29-2023 01:43:46 AM [AppRunner] The specified runtime version is not supported. Refer to the Release information in the App Runner Developer guide for supported runtime versions.

所以我对在 AWS apprunner 中安装虚拟环境和版本控制问题感到困惑。不过,我按照此博客在 AWS App Runner 上部署和扩展 Django 应用程序。

python django amazon-web-services virtualenv version
1个回答
0
投票

改变这个

build:
  - pip install pipenv
  - pipenv install -r requirements.txt

对此

build:
  - python -m venv /path/to/new/virtual/environment
  - cd /path/to/new/virtual/environment
  - Scripts\activate
  - pip install -r requirements.txt
© www.soinside.com 2019 - 2024. All rights reserved.