【问题标题】:Dockerised microservice giving no responseDockerised 微服务没有响应
【发布时间】:2019-03-15 10:19:45
【问题描述】:

当我在http://localhost:8080 上浏览到应该在我的计算机中提供服务的应用程序时,我没有得到任何 http 应答。这里出了什么问题?

我已经构建了一个烧瓶应用程序来查询服务器并返回结果。当我使用 gunicorn app:app --config=config.py 运行 gunicorn 时,应用程序在本地运行良好,但当我使用命令 docker run IMAGE 运行 docker 映像时,即使应用程序在我得到输出时似乎正在运行:

[2019-03-15 10:13:06 +0000] [1] [INFO] Starting gunicorn 19.9.0
[2019-03-15 10:13:06 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
[2019-03-15 10:13:06 +0000] [1] [INFO] Using worker: threads
[2019-03-15 10:13:06 +0000] [8] [INFO] Booting worker with pid: 8
[2019-03-15 10:13:06 +0000] [10] [INFO] Booting worker with pid: 10
[2019-03-15 10:13:06 +0000] [14] [INFO] Booting worker with pid: 14
[2019-03-15 10:13:06 +0000] [15] [INFO] Booting worker with pid: 15
[2019-03-15 10:13:06 +0000] [17] [INFO] Booting worker with pid: 17
[2019-03-15 10:13:06 +0000] [18] [INFO] Booting worker with pid: 18
[2019-03-15 10:13:07 +0000] [23] [INFO] Booting worker with pid: 23
[2019-03-15 10:13:07 +0000] [24] [INFO] Booting worker with pid: 24
[2019-03-15 10:13:07 +0000] [30] [INFO] Booting worker with pid: 30

app.py 文件是:

import logging

from flask import Flask, request, jsonify
from config import auth_secret_token, PORT, DEBUG_MODE
from helper import check_parameters
from object_detection_grpc_client import main


app = Flask(__name__)


def check_authorization(request):
    try:
        if not 'Auth-token' in request.headers:
            return jsonify({'error': 'unauthorized access'}), 401
        token = request.headers['Auth-token']
        if token != auth_secret_token:
            return jsonify({'error': 'unauthorized access'}), 401
        return "ok", 200
    except Exception as e:
        return jsonify({'error': 'unauthorized access'}), 401


@app.route("/", methods=['POST'])
def hello():
    info, status_code = check_authorization(request)

    if status_code != 200:
        return info, status_code
    else: 
        status, status_code = check_parameters(request.form)

    if status_code != 200:
        return status, status_code
    else:
        score = main()
        response = {"status": "success", "score": score, "customer_id":(request.form["cust_id"])}

        return jsonify(response), status_code


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=PORT, debug=DEBUG_MODE)

config.py 文件是:

from os import environ as env
import multiprocessing

PORT = int(env.get("PORT", 8080))
DEBUG_MODE = int(env.get("DEBUG_MODE", 1))

# Gunicorn config
bind = ":" + str(PORT)
workers = multiprocessing.cpu_count() * 2 + 1
threads = 2 * multiprocessing.cpu_count()

auth_secret_token = "token"
server='A.B.C.D:9000'
model_name="mymodel"
input_image='filename.jpg'
label_map="./data/object_detection.pbtxt"

Dockerfile 是:

FROM python:3.5.2
RUN apt update
WORKDIR /app
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
ADD . /app
ENV PORT 8080
CMD ["gunicorn", "app:app", "--config=config.py"]

文件夹结构是:

.
├── app.py
├── app.yaml
├── config.py
├── data
│   └── object_detection.pbtxt
├── Dockerfile
├── filename.jpg
├── helper.py
├── object_2.py
├── object_detection
│   ├── core
│   │   ├── anchor_generator.py

│       └── vrd_evaluation_test.py
├── object_detection_grpc_client.py

├── requirements.txt
└── tensorflow_serving
    ├── apis

       └── regression_pb2.py.
├── app.py
├── app.yaml
├── config.py
├── data
│   └── object_detection.pbtxt
├── Dockerfile
├── filename.jpg
├── helper.py
├── object_2.py
├── object_detection
│   ├── core
│   │   ├── anchor_generator.py

│       └── vrd_evaluation_test.py
├── object_detection_grpc_client.py

├── requirements.txt
└── tensorflow_serving
    ├── apis

       └── regression_pb2.py

【问题讨论】:

  • 你绑定了docker端口吗?我觉得可能缺少那个,你的docker run 命令是什么?
  • @AlejandroVales 我使用docker run 5909eedf6906,其中 5909eedf6906 是图像 ID

标签: python docker flask gunicorn


【解决方案1】:

我觉得这里发生的事情是您没有将端口绑定到您的计算机from the docker documentation this is how you expose it

--publish , -p 将容器的端口发布到主机

尝试使用以下命令运行 docker 容器

docker run -p 8080:8080 IMAGE

然后查询http://localhost:8080

【讨论】:

  • 谢谢!这在本地解决了这个问题,但是当我在 Google Kubernetes Engine 上推送图像时,我遇到了另一个与 gunicorn 相关的问题,我得到了这个 stackoverflow 问题stackoverflow.com/questions/55178800/… 中所示的错误。你能帮忙吗?
猜你喜欢
  • 1970-01-01
  • 2020-02-06
  • 2021-03-02
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
  • 2017-03-31
相关资源
最近更新 更多