Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Configuring Gunicorn for Development and Production with Env Variables

configuring-gunicorn-for-development-and-production-with-env-variables.jpg

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:

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.

# Demo Video

Code Snippets

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 config/gunicorn.py:

# -*- 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 .env file:

# 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

Reference Links

What are your favorite gunicorn config options? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments