简体   繁体   English

Dockerfile 没有安装 python-dotenv 库

[英]There is no python-dotenv library installed by Dockerfile

My Dockerfile:我的 Dockerfile:

FROM python:3.9

WORKDIR /usr/src/app

RUN pip install python-dotenv
RUN pip install other_libraries...

During building container, message is displayed:在构建容器期间,显示消息:

...
Step 3/4 : RUN pip install python-dotenv
---> Running in 5fffd3fe4042
Collecting python-dotenv
Downloading python_dotenv-0.15.0-py2.py3-none-any.whl (18 kB)
Installing collected packages: python-dotenv
Successfully installed python-dotenv-0.15.0
Removing intermediate container 5fffd3fe4042
---> 2cd0942f520c
...

But when I run docker-compose exec container_name pip list there is no on list python-dotenv library.但是当我运行docker-compose exec container_name pip list中没有python-dotenv库。

I tried on python:3.9 , python:3.8 with python-dotenv in 0.14 or 0.15 version.我在 0.14 或 0.15 版本中尝试使用python:3.9python:3.8python-dotenv

Of course, when I run docker-compose exec container_name pip install python-dotenv everything is okey.当然,当我运行docker-compose exec container_name pip install python-dotenv一切正常。

Why RUN command in Dockerfile not installed correctly?为什么未正确安装 Dockerfile 中的RUN命令?

Step by step.一步步。

  1. Build image that including python libraries (using your Dockerfile)构建包含 python 库的镜像(使用你的 Dockerfile)
docker build -t dotenv_image:1.0 .
  1. List images列出图像
docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dotenv_image              1.0                 507f2d0505a0        4 minutes ago       891MB

↑Okay it is here ↑好的,到了

  1. Run container with sh shell to be able run commands and check what inside container使用sh shell 运行容器以能够运行命令并检查容器内的内容
docker run --rm -it --entrypoint sh dotenv_image:1.0
  1. Check libraries检查库
pip freeze
python-dotenv==0.15.0

Library is here图书馆在这里

  1. Try to use library尝试使用库
python -c 'from dotenv import load_dotenv; print("ALL OK" if load_dotenv() else "CAN NOT LOAD")';
ALL OK

↑Positive output ↑正输出

I don't use docker-compose, but since your title says that your Dockerfile isn't working right, I can tell you that that's apparently not the problem.我不使用 docker-compose,但是由于您的标题说您的 Dockerfile 无法正常工作,我可以告诉您这显然不是问题。 Your Dockerfile seems to work just fine:您的 Dockerfile 似乎工作得很好:

>>> cat Dockerfile
FROM python:3.9
WORKDIR /usr/src/app
RUN pip install python-dotenv

>>> docker build -t so2 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM python:3.9
3.9: Pulling from library/python
e4c3d3e4f7b0: Pull complete
101c41d0463b: Pull complete
8275efcd805f: Pull complete
751620502a7a: Pull complete
0a5e725150a2: Pull complete
397dba5694db: Pull complete
b1d09d0eabcb: Pull complete
475299e7c7f3: Pull complete
d2fe14d8e6bc: Pull complete
Digest: sha256:429b2fd1f6657e4176d81815dc9e66477d74f8cbf986883c024c9b97f7d4d5a6
Status: Downloaded newer image for python:3.9
 ---> 5336a27a9b1f
Step 2/3 : WORKDIR /usr/src/app
 ---> Running in 37b03142a9b6
Removing intermediate container 37b03142a9b6
 ---> 4677ab34ce84
Step 3/3 : RUN pip install python-dotenv
 ---> Running in e89d17be1a32
Collecting python-dotenv
  Downloading python_dotenv-0.15.0-py2.py3-none-any.whl (18 kB)
Installing collected packages: python-dotenv
Successfully installed python-dotenv-0.15.0
Removing intermediate container e89d17be1a32
 ---> 55d00eeae4b4
Successfully built 55d00eeae4b4
Successfully tagged so2:latest

>>> docker run -it so2 bash
root@d211989c4bd7:/usr/src/app# pip list
Package       Version
------------- -------
pip           20.2.4
python-dotenv 0.15.0
setuptools    50.3.2
wheel         0.35.1
root@d211989c4bd7:/usr/src/app# exit

>>> docker run -it so2
Python 3.9.0 (default, Oct 13 2020, 20:14:06)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from dotenv import load_dotenv; load_dotenv()
True
>>>

>>> represent my prompt in my MacBook pro Terminal window. >>>在我的 MacBook pro 终端窗口中代表我的提示。

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

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