如何在docker上运行gunicorn

当docker启动时,我有两个依赖于彼此的文件。 1是一个烧瓶文件,一个是具有几个function的文件。 当docker启动时,只有函数文件会被执行,但是它会从flask文件中导入烧瓶variables。 例:

Flaskfile

import flask from flask import Flask, request import json _flask = Flask(__name__) @_flask.route('/', methods = ['POST']) def flask_main(): s = str(request.form['abc']) ind = global_fn_main(param1,param2,param3) return ind def run(fn_main): global global_fn_main global_fn_main = fn_main _flask.run(debug = False, port = 8080, host = '0.0.0.0', threaded = True) 

主文件

 import flaskfile #a few functions then if__name__ == '__main__': flaskfile.run(main_fn) 

剧本运行良好,不需要gunicorn。

Dockerfile

  FROM python-flask ADD *.py *.pyc /code/ ADD requirements.txt /code/ WORKDIR /code EXPOSE 8080 CMD ["python","main_file.py"] 

在命令行中:我通常做: docker run -it -p 8080:8080 my_image_name然后docker run -it -p 8080:8080 my_image_name会启动并监听。

现在要使用gunicorn:我试图修改我的CMD参数在dockerfile中

 ["gunicorn", "-w", "20", "-b", "127.0.0.1:8083", "main_file:flaskfile"] 

但它只是保持退出。 我不是写docker枪炮的命令吗?

这是我使用Django App的Dockerfile的最后一部分

 EXPOSE 8002 COPY entrypoint.sh /code/ WORKDIR /code ENTRYPOINT ["sh", "entrypoint.sh"] 

然后在entrypoint.sh中

 #!/bin/bash # Prepare log files and start outputting logs to stdout mkdir -p /code/logs touch /code/logs/gunicorn.log touch /code/logs/gunicorn-access.log tail -n 0 -f /code/logs/gunicorn*.log & export DJANGO_SETTINGS_MODULE=django_docker_azure.settings exec gunicorn django_docker_azure.wsgi:application \ --name django_docker_azure \ --bind 0.0.0.0:8002 \ --workers 5 \ --log-level=info \ --log-file=/code/logs/gunicorn.log \ --access-logfile=/code/logs/gunicorn-access.log \ "$@" 

希望这可能是有用的