how to run gunicorn on docker

后端 未结 6 452
南旧
南旧 2020-12-29 08:06

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

相关标签:
6条回答
  • 2020-12-29 08:44

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

    0 讨论(0)
  • 2020-12-29 08:50

    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" ]
    
    0 讨论(0)
  • 2020-12-29 08:50

    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. Also, don't forget the shebang on the top, #!/usr/bin/env python if you are running the Debug LOG-LEVEL.

    0 讨论(0)
  • 2020-12-29 08:51

    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:

    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.

    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.

    Note that when run by Gunicorn, __name__ is not "main". In my example it is equal to "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:

    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.

    0 讨论(0)
  • 2020-12-29 08:56

    This is my last part of my Dockerfile with Django App

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

    then in 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 \
    "$@"
    

    Hope this could be useful

    0 讨论(0)
  • 2020-12-29 09:00

    After struggling with this issue over the last 3 days, I found that all you need to do is to bind to loopback rather than localhost:

    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:

    EXPOSE 8000
    

    Now:

    docker build -t test .
    

    Finally you can run:

    docker run -d -p 8000:8000 test
    
    0 讨论(0)
提交回复
热议问题