使用pipenv,我有以下pipfile:
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
flask = "*"
uuid = "*"
gunicorn = "*"
psycopg2-binary = "*"
[dev-packages]
[requires]
python_version = "3.8"
然后我的Dockerfile设置如下:
FROM python:3.8.3-slim-buster
RUN useradd deploy_trial
WORKDIR /home/deploy_trial
RUN pip install pipenv
COPY . /home/deploy_trial/
RUN pipenv install --deploy
CMD ["python","./app/text.py"]
然而,尽管构建似乎很成功,但当我运行映像时,我收到一条错误消息,上面写着:
Traceback (most recent call last):
File "./app/text.py", line 1, in <module>
import psycopg2
ModuleNotFoundError: No module named 'psycopg2'
所以它显然认为psycopg2还没有安装。。。这真的很奇怪,因为当我在本地机器上使用psycopg2二进制文件时,当代码运行时,那里的所有程序都会将安装识别为psycopg2。有人知道怎么解决这个问题吗?
我的dockerfile:
FROM python:3.8.3-slim-buster
RUN python -m pip install pipenv
COPY script.py script.py
COPY Pipfile Pipfile
RUN pipenv install
CMD ["pipenv", "run", "python", "script.py"]
script.py:
import psycopg2
print('it is ok')
Pipfile:
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
psycopg2-binary = "*"
[dev-packages]
[requires]
python_version = "3.8"
它适用于这样的配置。