简体   繁体   English

如何在 docker 上运行 gunicorn

[英]how to run gunicorn on docker

I have 2 files that depend on each other when docker is start up.当 docker 启动时,我有 2 个相互依赖的文件。 1 is a flask file and one is a file with a few functions. 1是一个flask文件,一个是有几个功能的文件。 When docker starts, only the functions file will be executed but it imports flask variables from the flask file.当 docker 启动时,只会执行函数文件,但它会从烧瓶文件中导入烧瓶变量。 Example:例子:

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)

Main File主文件

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

The script runs fine without need a gunicorn.脚本运行良好,不需要 gunicorn。

Dockerfile文件

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

In the Command line: i usally do: docker run -it -p 8080:8080 my_image_name and then docker will start and listen.在命令行中:我通常这样做: docker run -it -p 8080:8080 my_image_name然后docker run -it -p 8080:8080 my_image_name将启动并监听。

Now to use gunicorn: I tried to modify my CMD parameter in the dockerfile to现在使用 gunicorn:我尝试将 dockerfile 中的CMD参数修改为

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

but it just keeps exiting.但它只是不断退出。 Am i not writing the docker gunicorn command right?我不是在写 docker gunicorn 命令吗?

I just went through this problem this week and stumbled on your question along the way.本周我刚刚解决了这个问题,并在此过程中偶然发现了你的问题。 Fair to say you either resolved this or changed approaches by now, but for future's sake:公平地说,你现在要么解决了这个问题,要么改变了方法,但为了未来:

The command in my Dockerfile is:我的 Dockerfile 中的命令是:

CMD ["gunicorn"  , "-b", "0.0.0.0:8000", "app:app"]

Where the first "app" is the module and the second "app" is the name of the WSGI callable, in your case, it should be _flask from your code although you've some other stuff going on that makes me less certain.第一个“应用程序”是模块,第二个“应用程序”是可调用 WSGI 的名称,在您的情况下,它应该是代码中的 _flask,尽管您还有其他一些事情让我不太确定。

Gunicorn takes the place of all the run statements in your code, if Flask's development web server and Gunicorn try to take the same port it can conflict and crash Gunicorn. Gunicorn 取代了代码中的所有运行语句,如果 Flask 的开发 Web 服务器和 Gunicorn 尝试使用相同的端口,它可能会发生冲突并导致 Gunicorn 崩溃。

Note that when run by Gunicorn, __name__ is not "main".请注意,当由__name__运行时, __name__不是“main”。 In my example it is equal to "app".在我的示例中,它等于“app”。

At my admittedly junior level of both Python, Docker, and Gunicorn the fastest way to debug is to comment out the "CMD" in the Dockerfile, get the container up and running:在我公认的 Python、Docker 和 Gunicorn 初级水平上,最快的调试方法是注释掉 Dockerfile 中的“CMD”,让容器启动并运行:

docker run -it -d -p 8080:8080 my_image_name

Hop onto the running container:跳上正在运行的容器:

 docker exec -it container_name /bin/bash

And start Gunicorn from the command line until you've got it working, then test with curl - I keep a basic route in my app.py file that just prints out "Hi" and has no dependencies for validating the server is up before worrying about the port binding to the host machine.并从命令行启动 Gunicorn 直到你让它工作,然后用 curl 进行测试 - 我在我的 app.py 文件中保留了一个基本路由,它只是打印出“Hi”并且在担心之前没有用于验证服务器是否启动的依赖项关于绑定到主机的端口。

After struggling with this issue over the last 3 days, I found that all you need to do is to bind to the non-routable meta-address 0.0.0.0 rather than the loopback IP 127.0.0.1 :在过去 3 天努力解决这个问题后,我发现您需要做的就是绑定到不可路由的元地址0.0.0.0而不是环回 IP 127.0.0.1

CMD ["gunicorn" , "--bind", "0.0.0.0:8000", "app:app"]

And don't forget to expose the port, one option to do that is to use EXPOSE in your Dockerfile:并且不要忘记公开端口,这样做的一种选择是在您的 Dockerfile 中使用 EXPOSE:

EXPOSE 8000

Now:现在:

docker build -t test .

Finally you can run:最后你可以运行:

docker run -d -p 8000:8000 test

This is my last part of my Dockerfile with Django App这是我的 Dockerfile 与 Django App 的最后一部分

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

then in entrypoint.sh然后在入口点.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 \
"$@"

Hope this could be useful希望这可能有用

This work for me:这对我有用:

FROM docker.io/python:3.7

WORKDIR /app

COPY requirements.txt ./

RUN pip install --no-cache-dir -r requirements.txt

ENV GUNICORN_CMD_ARGS="--bind=0.0.0.0 --chdir=./src/"
COPY . .

EXPOSE 8000

CMD [ "gunicorn", "app:app" ]

gunicorn main:app --workers 4 --bind :3000 --access-logfile '-'

I was trying to run a flask app as well.我也试图运行一个烧瓶应用程序。 I found out that you can just use我发现你可以使用

ENTRYPOINT['gunicorn', '-b', ':8080', 'app:APP']

This will take take the file you have specified and run on the docker instance.这将采用您指定的文件并在 docker 实例上运行。 Also, don't forget the shebang on the top, #!/usr/bin/env python if you are running the Debug LOG-LEVEL.另外,如果您正在运行调试日志级别,请不要忘记顶部的 shebang, #!/usr/bin/env python

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

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