2017-05-11 40 views
0

當docker啓動時,我有兩個依賴於彼此的文件。 1是一個燒瓶文件,一個是具有幾個功能的文件。當docker啓動時,只有函數文件會被執行,但是它會從flask文件中導入瓶子變量。例如:如何在碼頭上運行gunicorn

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"] 

在命令行:我usally做到: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 Gunicorn命令嗎?

+0

什麼錯誤,你看到當容器退出? –

回答

0

這與Django應用程序

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 \ 
"[email protected]" 

希望我的我的Dockerfile的最後一部分,這可能是有用的

+0

這部分在我的末端看起來像什麼? 'exec execicorn django_docker_azure.wsgi:application'我可以使用'exec gunicorn my_docker_image.wsgi:application'嗎? – jxn

+0

該部分是與Django應用程序有關,我vern使用燒瓶,所以我不能給你提示。抱歉 –