Configuring Gunicorn for Development and Production with Env Variables
In this video we'll cover how to use the same gunicorn config file in dev and prod but still be able to tweak settings with env variables.
Quick Jump: Demo Video
I like to keep my development set up as close to production as possible and using environment variables is a great way to tweak a few settings depending on which environment I’m in without having to duplicate config files.
Launching gunicorn from within a Dockerfile:
CMD ["gunicorn", "-c", "python:config.gunicorn", "snakeeyes.app:create_app()"]
Launching gunicorn without Docker:
gunicorn -c "python:config.gunicorn" "snakeeyes.app:create_app()"
The gunicorn config file in
# -*- coding: utf-8 -*- import multiprocessing import os from distutils.util import strtobool bind = os.getenv('WEB_BIND', '0.0.0.0:8000') accesslog = '-' access_log_format = "%(h)s %(l)s %(u)s %(t)s '%(r)s' %(s)s %(b)s '%(f)s' '%(a)s' in %(D)sµs" workers = int(os.getenv('WEB_CONCURRENCY', multiprocessing.cpu_count() * 2)) threads = int(os.getenv('PYTHON_MAX_THREADS', 1)) reload = bool(strtobool(os.getenv('WEB_RELOAD', 'false')))
A few of the environment variables in the
# Which address and port should gunicorn bind to? #WEB_BIND=0.0.0.0:8000 # Do you want code reloading to work with your app server? Don't do this in # production (it's turned off by default, so don't worry about it). WEB_RELOAD=true # How many workers and threads should your app use? WEB_CONCURRENCY=1 #PYTHON_MAX_THREADS=1
What are your favorite gunicorn config options? Let me know below.