我有一个Python3 Flask应用程序,我想将其放入容器中。我已经将Flask添加到requirements.txt,并且可以在本地工作,但是在容器中找不到“ flask”模块。我在做什么错了?
requirements.txt:
aniso8601==7.0.0
certifi==2019.6.16
chardet==3.0.4
Click==7.0
Flask==1.1.1
Flask-RESTful==0.3.7
idna==2.8
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
pymongo==3.8.0
pytz==2019.1
requests==2.22.0
six==1.12.0
urllib3==1.25.3
Werkzeug==0.15.4
最低工作app.py
:
from flask import Flask
from flask_restful import Api
app = Flask(__name__)
api = Api(app)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8080)
Dockerfile:
FROM python:3.7-alpine
WORKDIR /usr/src/app
RUN apk add --no-cache \
uwsgi-python3
COPY . .
ENV TESTING TRUE
RUN pip3 install --no-cache-dir -r requirements.txt
CMD [ "uwsgi", "--socket", "0.0.0.0:3031", \
"--uid", "uwsgi", \
"--plugins", "python3", \
"--protocol", "uwsgi", \
"--wsgi", "app:app", \
"-p", "4", \
"--enable-threads"]
错误:
*** Operational MODE: preforking ***
Traceback (most recent call last):
File "./app.py", line 5, in <module>
from flask import Flask, request, Response
ModuleNotFoundError: No module named 'flask'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
Example of app.py(太大而无法添加到问题中)
项目结构:
├── app.py
├── docker-compose.yml
├── Dockerfile
├── LICENSE
├── logging_config.ini
├── nginx.conf
├── __pycache__
│ └── utils.cpython-37.pyc
├── README.md
├── requirements.txt
├── testing_data
│ ├─- some jsons here
├── tests.py
└── utils.py
答案 0 :(得分:1)
尝试使用pip代替pip3,然后尝试将requirements.txt复制到Docker根目录。 另外,在使用工作目录之前,还需要创建一个工作目录。
类似的东西:
FROM python:3.7-alpine
COPY requirements.txt /
RUN pip install --no-cache-dir -r requirements.txt
RUN mkdir /app
WORKDIR /app
COPY . app/
# not sure what this is doing
RUN apk add --no-cache \
uwsgi-python3
# ENV TESTING TRUE
CMD [ "uwsgi", "--socket", "0.0.0.0:3031", \
"--uid", "uwsgi", \
"--plugins", "python3", \
"--protocol", "uwsgi", \
"--wsgi", "app:app", \
"-p", "4", \
"--enable-threads"]```
答案 1 :(得分:1)
我已经尝试过复制错误(没有运气),但是您可能仍然觉得这很有用。如果您可以使用app.py
第1-5行来编辑问题,那么我也许可以复制并更新答案。
我已经修改了包含bash的Dockerfile(第5行),以便您可以进入容器并查看发生了什么-
FROM python:3.7-alpine
WORKDIR /usr/src/app
RUN apk add --no-cache \
uwsgi-python3 \
bash
COPY . .
ENV TESTING TRUE
RUN pip3 install --no-cache-dir -r requirements.txt
CMD [ "uwsgi", "--socket", "0.0.0.0:3031", \
"--uid", "uwsgi", \
"--plugins", "python3", \
"--protocol", "uwsgi", \
"--wsgi", "app:app", \
"-p", "4", \
"--enable-threads"]
重建图像
$ docker build . -t soflask
将容器作为守护程序运行
$ docker run -d --rm soflask
$ docker ps
CONTAINER ID ...
96f341e998d4 ...
$ docker exec -i -t 96f bash
$ python
>>> from flask import Flask
>>>
没有错误... app.py第1-5行是什么样的?
答案 2 :(得分:0)
好吧,运行 alpine:3.10 会有所帮助,但是因为它当前随Python 3.6一起提供(我需要3.7),所以我以这样的Dockerfile结尾:
FROM alpine:3.10
VOLUME /usr/src/app/public
WORKDIR /usr/src/app
RUN apk add --no-cache python3 && \
if [ ! -e /usr/bin/python ]; then ln -sf python3 /usr/bin/python ; fi && \
python3 -m ensurepip && \
rm -r /usr/lib/python*/ensurepip && \
pip3 install --no-cache --upgrade pip setuptools wheel && \
if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi
RUN apk add --no-cache \
uwsgi-python3
COPY . .
RUN rm -rf public/*
ENV TESTING TRUE
RUN pip3 install --no-cache-dir -r requirements.txt
CMD [ "uwsgi", "--socket", "0.0.0.0:3031", \
"--uid", "uwsgi", \
"--plugins", "python3", \
"--protocol", "uwsgi", \
"--wsgi", "app:app", \
"-p", "4", \
"--enable-threads"]
此 RUN APK添加--no-cache python3 ... 魔术来自here。不知道如何,但是它只是有效。