Dockerfile没有安装python-dotenv库

问题描述

我的Dockerfile:

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

在建筑物容器中,显示消息:

docker-compose exec container_name pip list

但是当我运行python-dotenv时,列表python:3.9中没有库。

我尝试使用python:3.8的{​​{1}},python-dotenv和0.14或0.15版本。

当然,当我运行docker-compose exec container_name pip install python-dotenv时,一切都很好。

为什么Dockerfile中的RUN命令未正确安装?

解决方法

逐步。

  1. 使用Dockerfile构建包含python库的映像
preventDefault()
  1. 列出图片
SELECT TO_CHAR(column_name,'99G999D99MI')
as format_column
FROM DUAL;
docker build -t dotenv_image:1.0 .

↑好吧,在这里

  1. 使用docker images shell运行容器以能够运行命令并检查容器中的内容
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dotenv_image              1.0                 507f2d0505a0        4 minutes ago       891MB
  1. 检查库
sh
docker run --rm -it --entrypoint sh dotenv_image:1.0

图书馆在这里

  1. 尝试使用库
pip freeze
python-dotenv==0.15.0

↑正输出

,

我不使用docker-compose,但是由于您的标题显示Dockerfile不能正常工作,所以我可以告诉您,这显然不是问题。您的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
>>>

>>>代表我在MacBook Pro终端窗口中的提示。