Python 如何在 docker 上运行 gunicorn
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43925487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to run gunicorn on docker
提问by jxn
I have 2 files that depend on each other when docker is start up. 1 is a flask file and one is a file with a few functions. When docker starts, only the functions file will be executed but it imports flask variables from the flask file. Example:
当 docker 启动时,我有 2 个相互依赖的文件。1是一个flask文件,一个是有几个功能的文件。当 docker 启动时,只会执行函数文件,但它会从烧瓶文件中导入烧瓶变量。例子:
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 将启动并监听。
Now to use gunicorn:
I tried to modify my CMD
parameter in the dockerfile to
现在使用 gunicorn:我尝试CMD
将 dockerfile 中的参数修改为
["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 命令吗?
回答by user7504939
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 shouldbe _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". In my example it is equal to "app".
请注意,当由 Gunicorn 运行时,__name__
不是“主要的”。在我的示例中,它等于“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”并且在担心之前没有用于验证服务器是否启动的依赖项关于绑定到主机的端口。
回答by e.arbitrio
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
希望这可能有用
回答by burtsevyg
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" ]
回答by user8926546
gunicorn main:app --workers 4 --bind :3000 --access-logfile '-'
gunicorn main:app --workers 4 --bind :3000 --access-logfile '-'
回答by Shreyaa Sridhar
I was trying to run a flask app as well. I found out that you can just use
我也试图运行一个烧瓶应用程序。我发现你可以使用
ENTRYPOINT['gunicorn', '-b', ':8080', 'app:APP']
ENTRYPOINT['gunicorn', '-b', ':8080', 'app:APP']
This will take take the file you have specified and run on the docker instance. Also, don't forget the shebang on the top, #!/usr/bin/env python
if you are running the Debug LOG-LEVEL.
这将采用您指定的文件并在 docker 实例上运行。另外,#!/usr/bin/env python
如果您正在运行调试日志级别,请不要忘记顶部的shebang 。