我目前有一个用Flask编写的小型api,用于与CNN交互,我正在设置配置以在Docker中运行它,并且一切运行正常,这是我的实际配置:
Dockerfile:
FROM python:2.7.15-jessie
RUN mkdir -p usr/src/app
COPY . usr/src/app
WORKDIR usr/src/app
RUN which python
RUN apt-get update && apt-get install -y
RUN pip install flask flask_uploads Werkzeug opencv-python numpy tensorflow
ENV PORT 5000
EXPOSE 5000
CMD ["flask", "run"]
Docker-compose:
version: "2.2"
services:
api:
container_name: "pyserver"
build: .
volumes:
- ".:/usr/src/app"
environment:
FLASK_APP: server.py
ports:
- "5000:5000"
command: ["flask", "run"]
server.py
import os
from base64 import b64encode, b64decode
from flask import Flask, redirect, request, url_for, json, send_file
from flask_uploads import UploadSet, configure_uploads
from werkzeug.utils import secure_filename
from GW_predict import predict
UPLOAD_FOLDER = 'CNN/Files'
ALLOWED_EXTENSIONS = set(['txt', 'csv', 'png', 'jpg', 'jpeg'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config["DEBUG"] = True
def allowed_file(filename):
ext = '.' in filename and \
filename.rsplit('.', 1)[1].lower()
return ext, ext in ALLOWED_EXTENSIONS
@app.route('/status', methods=['GET'])
def status():
return create_response({ 'online': True, 'message': 'UP AND RUNNING @ 5000' }, 200)
@app.route('/uploadFile', methods=['POST'])
def upload_file():
if 'h1' not in request.files or 'l1' not in request.files:
return create_response({'result': False, 'message': 'File missing'}, 422)
h1_file = request.files['h1']
l1_file = request.files['l1']
if h1_file.filename == '' or l1_file.filename == '':
return create_response({'result': False, 'message': 'File missing'}, 422)
h1_ext, h1res = allowed_file(h1_file.filename)
l1_ext, l1res = allowed_file(l1_file.filename)
if h1_file and l1_file and h1res and l1res:
h1_filename = secure_filename(h1_file.filename)
l1_filename = secure_filename(l1_file.filename)
h1 = os.path.join(app.config['UPLOAD_FOLDER'], h1_filename)
l1 = os.path.join(app.config['UPLOAD_FOLDER'], l1_filename)
h1_file.save(h1)
l1_file.save(l1)
# img = b64encode((open(img, "rb").read()))
if h1_ext == 'png' and l1_ext == 'png':
result = predict(h1, l1)
return create_response({'result': True, 'prediction': result}, 200)
return create_response({'result': False, 'message': 'Images format must be png'}, 422)
return create_response({'result': False, 'message': 'No allowed file'}, 422)
def create_response(message, status):
response = app.response_class(
response= json.dumps(message),
status= status,
mimetype='application/json'
)
return response
if __name__ == '__main__':
app.run()
问题是,在我的docker-compose文件中,我配置了“端口”指令,我在其中定义了在主机中公开端口5000并转发到容器中的同一端口。
这不起作用。
在容器内部,我可以通过CURL向端点发出请求,但是在容器外部,我无法这样做。怎么了?
答案 0 :(得分:2)
也许您需要指定--host=0.0.0.0
标志? (来自here)。
您可以尝试在docker-compose中覆盖command
吗?
version: "2.2"
services:
api:
container_name: "pyserver"
build: .
command: flask run --host=0.0.0.0
volumes:
- ".:/usr/src/app"
environment:
FLASK_APP: server.py
ports:
- "5000:5000"
command: ["flask", "run"]
顺便说一句,如果仅从主机(而不是其他容器)进行通信,则不确定dockerfile中的EXPOSE
是否必要。