简体   繁体   English

如何在docker容器中导入python模块运行脚本?

[英]How to import python module running script in docker container?

I need to run python script in the docker container.我需要在 docker 容器中运行 python 脚本。 This script has import of the psutil module.该脚本导入了psutil模块。 I created image and after running it I'm getting error:我创建了图像,运行后出现错误:

File "/usr/src/app/metrics.py", line 1, in <module>
   import psutil
ModuleNotFoundError: No module named 'psutil'

Locally my script works cause i installed this module through "python -m pip install psutil" command.我的脚本在本地工作是因为我通过“python -m pip install psutil”命令安装了这个模块。 I've tried to simulate this action in dockerfile through: "CMD python -m pip install psutil" but it doesn`t work.我试图通过以下方式在 dockerfile 中模拟此操作: "CMD python -m pip install psutil"但它不起作用。 What am i doing wrong?我究竟做错了什么? Dockerfile content: Dockerfile 内容:

FROM python:3.8.2-buster
WORKDIR /usr/src/app
COPY metrics.py .
RUN pip install --upgrade pip
CMD python -m pip install psutil
CMD python /usr/src/app/metrics.py

According to https://docs.docker.com/engine/reference/builder/ "There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect."根据https://docs.docker.com/engine/reference/builder/ “一个 Dockerfile 中只能有一个 CMD 指令。如果你列出了多个 CMD,那么只有最后一个 CMD 才会生效。”

Maybe you just want to install psutil using another RUN ?也许您只想使用另一个RUN安装psutil

FROM python:3.8.2-buster
WORKDIR /usr/src/app
COPY metrics.py .
RUN pip install --upgrade pip
RUN python -m pip install psutil
CMD python /usr/src/app/metrics.py

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

相关问题 在Docker容器中运行python脚本时导入错误? - Import error when running python script inside docker container? 在Docker容器中运行python脚本时没有json模块 - no json module when running python script in docker container Python 模块未在 docker 容器中运行 - Python module not running in docker container 如何将参数传递给在 docker 容器内运行的 python 脚本? - How to pass arguments to the python script that is running inside docker container? 将参数传递给在Docker容器中运行的python脚本 - Pass argument to python script running in a docker container 在 nginx docker 容器内运行 python 脚本 - Running a python script inside an nginx docker container Docker - 在可执行的Python脚本上导入本地模块 - Docker - Import local module on executable Python script 如何从在Docker容器中运行的python脚本(使用docker sdk)创建映像? - How to create an image from an python script (using docker sdk) running in docker container? 使用 Python 为 AWS Lambda 构建 Docker 容器时出现导入模块错误 - Import Module Error when building a Docker Container with Python for AWS Lambda 如何通过运行带有URL的docker容器来连接docker容器中的python应用 - how to connect python app in docker container with running docker container with url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM