|
1 | 1 | # Docker Python Waitress |
2 | 2 |
|
3 | | -Docker container to run a WSGI Python app using Waitress. |
| 3 | +Docker container to run a WSGI Python app using |
| 4 | +[Waitress](https://docs.pylonsproject.org/projects/waitress/en/stable/index.html). Images support python 3.6+ and are |
| 5 | +based on the official python-slim containers. |
| 6 | + |
| 7 | +[Pull from Docker Hub](https://hub.docker.com/r/tecktron/python-waitress/) |
| 8 | + |
| 9 | +[View on GitHub](https://github.com/Tecktron/docker-python-waitress) |
| 10 | + |
| 11 | + |
| 12 | +## How to use |
| 13 | + |
| 14 | +* You don't need to clone the GitHub repo. You can use this image as a base image for other images, using this in your `Dockerfile`: |
| 15 | + |
| 16 | +```Dockerfile |
| 17 | +FROM tecktron/python-waitress:latest |
| 18 | + |
| 19 | +COPY ./ /app |
| 20 | +``` |
| 21 | + |
| 22 | +It will expect a file at `/app/app/wsgi.py`. |
| 23 | + |
| 24 | +Or otherwise a file at `/app/wsgi.py`. |
| 25 | + |
| 26 | +And will expect it to contain a variable `application` with your "WSGI" application. |
| 27 | + |
| 28 | +Then you can build your image from the directory that has your `Dockerfile`, e.g: |
| 29 | + |
| 30 | +```bash |
| 31 | +docker build -t myimage ./ |
| 32 | +``` |
| 33 | + |
| 34 | +## Options |
| 35 | + |
| 36 | +All options can be set using environment variables. These can be passed either in a wrapper dockerfile, passing in a .env file or passing them with the |
| 37 | +-e flag to the docker call. |
| 38 | + |
| 39 | +### Prestart Script |
| 40 | +If you need to run any startup commands before waitress runs (an example might be running migrations) you can override the `prestart.sh` script. This script should live within the `/app` directory in the container. The image will automatically detect and run it before starting waitress. |
| 41 | + |
| 42 | + |
| 43 | +### Variables |
| 44 | + |
| 45 | +#### `MODULE_NAME` |
| 46 | + |
| 47 | +The Python "module" (file) to be imported by Gunicorn, this module would contain the actual application in a variable. |
| 48 | + |
| 49 | +By default: |
| 50 | + |
| 51 | +* `app.wsgi` if there's a file `/app/app/main.py` or |
| 52 | +* `wsgi` if there's a file `/app/wsgi.py` |
| 53 | + |
| 54 | +For example, if your main file was at `/app/custom_app/custom_script.py`, you could set it like: |
| 55 | + |
| 56 | +```bash |
| 57 | +docker run -d -p 80:80 -e MODULE_NAME="custom_app.custom_script" myimage |
| 58 | +``` |
| 59 | + |
| 60 | +#### `VARIABLE_NAME` |
| 61 | + |
| 62 | +The variable inside of the Python module that contains the WSGI application. |
| 63 | + |
| 64 | +By default: |
| 65 | + |
| 66 | +* `application` |
| 67 | + |
| 68 | +For example, if your main Python file has something like: |
| 69 | + |
| 70 | +```Python |
| 71 | +from flask import Flask |
| 72 | +api = Flask(__name__) |
| 73 | + |
| 74 | +@api.route("/") |
| 75 | +def hello(): |
| 76 | + return "Hello World from Flask" |
| 77 | +``` |
| 78 | + |
| 79 | +In this case `api` would be the variable with the "WSGI application". You could set it like: |
| 80 | + |
| 81 | +```bash |
| 82 | +docker run -d -p 80:80 -e VARIABLE_NAME="api" myimage |
| 83 | +``` |
| 84 | + |
| 85 | +#### `APP_MODULE` |
| 86 | + |
| 87 | +The string with the Python module and the variable name passed to Gunicorn. |
| 88 | + |
| 89 | +By default, set based on the variables `MODULE_NAME` and `VARIABLE_NAME`: |
| 90 | + |
| 91 | +* `app.wsgi:application` or |
| 92 | +* `wsgi:application` |
| 93 | + |
| 94 | +You can set it like: |
| 95 | + |
| 96 | +```bash |
| 97 | +docker run -d -p 80:80 -e APP_MODULE="custom_app.custom_script:api" myimage |
| 98 | +``` |
| 99 | + |
| 100 | +### Waitress Options |
| 101 | + |
| 102 | +#### Host and Port |
| 103 | +By default, Waitress has been setup to server on all hostnames on port 80 using both IPv4 and IPv6. This translates to `--listen:*:80`. This works for most applications using the basic setups listed above. |
| 104 | + |
| 105 | +You may have different needs so you can adjust and manipulate this by passing in environment variable to adjust the settings. |
| 106 | + |
| 107 | +There are 2 options for doing this. |
| 108 | +1. Pass a comma separated list of `host:port,host:port` to the `WAITRESS_LISTEN` param |
| 109 | +2. Pass the host and port separately as `WAITRESS_HOST` and/or `WAITRESS_PORT`. If port is left out, it will default to 80. |
| 110 | + |
| 111 | +The `WAITRESS_LISTEN` param takes precedence over `WAITRESS_HOST`/`WAITRESS_PORT` options, meaning if you include all 3, host and port settings will be ignored. |
| 112 | + |
| 113 | +##### Some examples |
| 114 | + |
| 115 | +To set waitress to use port 8080, sent the `WAITRESS_LISTEN` param like `-e WAITRESS_LISTEN=*:8080` |
| 116 | + |
| 117 | +If you want only IPv4, you could use advanced param listed in the section below, but you could also use `-e WAITRESS_HOST=0.0.0.0 -e WAITRESS_PORT=80` |
| 118 | + |
| 119 | + |
| 120 | +#### Advanced Options |
| 121 | + |
| 122 | +Many of the |
| 123 | +[supported options by waitress-serve](https://docs.pylonsproject.org/projects/waitress/en/stable/runner.html#invocation) |
| 124 | +are also supported by passing in environment variables. These params are only included in the call if they are included |
| 125 | +in the environment. The supported options are: |
| 126 | + |
| 127 | +| Environment Variable | Waitress Param | |
| 128 | +|:---------------------------------|:---------------------------------| |
| 129 | +| WAITRESS_EXPOSE_TRACEBACKS | --expose-tracebacks | |
| 130 | +| WAITRESS_NO_EXPOSE_TRACEBACKS | --no-expose-tracebacks | |
| 131 | +| WAITRESS_NO_IPV6 | --no-ipv6 | |
| 132 | +| WAITRESS_NO_IPV4 | --no-ipv4 | |
| 133 | +| WAITRESS_THREADS | --threads=`$VAL` | |
| 134 | +| WAITRESS_IDENT | --ident=`$VAL` | |
| 135 | +| WAITRESS_OUTBUF_OVERFLOW | --outbuf_high_watermark=`$VAL` | |
| 136 | +| WAITRESS_INBUF_OVERFLOW | --inbuf_overflow=`$VAL` | |
| 137 | +| WAITRESS_CONNECTION_LIMIT | --connection_limit=`$VAL` | |
| 138 | +| WAITRESS_MAX_REQUEST_HEADER_SIZE | --max_request_header_size=`$VAL` | |
| 139 | +| WAITRESS_MAX_REQUEST_BODY_SIZE | --max_request_body_size=`$VAL` | |
| 140 | +| WAITRESS_ASYNCORE_LOOP_TIMEOUT | --asyncore_loop_timeout=`$VAL` | |
| 141 | +| WAITRESS_ASYNCORE_USE_POLL | --asyncore_use_poll=`$VAL` | |
| 142 | + |
| 143 | +Where `$VAL` is the value passed into the environment. |
| 144 | +For example to pass in 5 threads, use `-e WAITRESS_THREADS=5` |
| 145 | + |
| 146 | +For those without any value, simply pass a 1. |
| 147 | +For example to turn off IPv6, use `-e WAITRESS_NO_IPV6=1` |
| 148 | + |
| 149 | +# Credits |
| 150 | +This dockerfile setup is based on https://github.com/tiangolo/meinheld-gunicorn-docker |
| 151 | + |
| 152 | +Waitress is one of the Pylons projects: https://pylonsproject.org |
0 commit comments