简体   繁体   中英

How to import python module running script in docker container?

I need to run python script in the docker container. This script has import of the psutil module. 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. I've tried to simulate this action in dockerfile through: "CMD python -m pip install psutil" but it doesn`t work. What am i doing wrong? Dockerfile content:

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."

Maybe you just want to install psutil using another RUN ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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