我有一个flask python 3.7应用程序,我想在docker和Kubernetes上运行。
我有以下Docker文件
这一项有效(使用python鸡蛋)
FROM python:3-stretch
WORKDIR /app
ADD . /app/
RUN pip install -e .
EXPOSE 5000
CMD ["python", "myapp/application.py"]
但这不是(但我想坚持下去)
FROM python:3-stretch
WORKDIR /app
ADD . /app/
RUN pip -r requirements.txt
EXPOSE 5000
CMD ["python", "myapp/application.py"]
运行此命令(在构建容器之后)
docker run -it -p 5000:5000 myapp-python:latest
出现以下错误
Traceback (most recent call last):
File "myapp/application.py", line 3, in <module>
import myapp.config as config
ModuleNotFoundError: No module named 'myapp'
但是如上所述,这与以前的Docker文件完全兼容。任何帮助深表感谢。
其他说明:
这是我的要求。txt
flask
requests
flask-restful
flask-migrate
flask-sqlalchemy
flask-marshmallow
flask-jwt-extended
marshmallow-sqlalchemy
python-dotenv
passlib
tox
neomodel==3.2.9
marshmallow>=3.0.0b11
apispec
phonenumbers
pillow
emailage>=1.1.2
emailage-official==1.0.2
boto3
geopy
fuzzywuzzy
py2neo>=4.1.0
python-dotenv
eth_utils
neo4j>=1.7.0
neotime>=1.7.1
python-jose
eth-utils
eth-hash[pycryptodome]
python-Levenshtein
jsonpatch
google-cloud-vision
google-cloud-translate
pycountry
sspyjose>=0.2.5
pysinglesource>=0.1.0
flask-cors
pynamodb
答案 0 :(得分:0)
我找到了解决方法。
root@64118c352a3e:/app# python
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/app', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']
然后在我的第二个配置中发现我在第二个容器中缺少'/app'
路径。
然后我使用添加路径
ENV PYTHONPATH "${PYTONPATH}:/app"
完整的Dockerfile如下所示。
FROM python:3-stretch
WORKDIR /app
ADD . /app/
RUN pip install -r requirements.txt
ENV PYTHONPATH "${PYTONPATH}:/app"
EXPOSE 5000
CMD ["python", "myapp/application.py"]
现在可以了。
希望这对某人有帮助