简体   繁体   English

为什么我的端口未在docker-compose中映射?

[英]Why are my ports not being mapped in docker-compose?

I currently have a small api written in Flask made for interacting with a CNN, I'm setting up the configuracion for running it in Docker and everything runs fine, this is my actual configuration: 我目前有一个用Flask编写的小型api,用于与CNN交互,我正在设置配置以在Docker中运行它,并且一切运行正常,这是我的实际配置:

Dockerfile: 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: Docker组成:

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 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()

The problem is that in my docker-compose file I have configured the "ports" instruction in which I define to expose de port 5000 in my host and to forward to the same port in the container. 问题是,在我的docker-compose文件中,我配置了“ ports”指令,在其中定义要在主机中公开端口5000并转发到容器中的同一端口。

This does not work. 这是行不通的。

Inside the container I can make the request via CURL to the endpoint, but outside of the container I am unable to do so. 在容器内部,我可以通过CURL向端点发出请求,但是在容器外部,我无法这样做。 What could be wrong? 有什么事吗

maybe you need to specify the --host=0.0.0.0 flag? 也许您需要指定--host=0.0.0.0标志? (from here ). (从这里开始 )。

Can you try to override your command in docker-compose? 您可以尝试在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"]

By the way, not sure that the EXPOSE in dockerfile is necessary if you are communicate only from your host machine (rather than other container). 顺便说EXPOSE ,如果仅从主机(而不是其他容器)进行通信,则不必确定EXPOSE中的EXPOSE是否必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM