Skip to contentSkip to conversation

uWSGI, and a docker-compose setup with nginx-proxy

RFC PATCH In review Tim Orling 2 participants 4 messages Nov 15 – Nov 16

lore
Share & tools
lore

Tim Orling

author
#0 lore raw

v2 cover uWSGI, and a docker-compose setup with nginx-proxy

This is an RFC: I run the layer index test instance I use for these patches this way, and would like to know if this is useful upstream, and in particular whether the production site would use it.

1: Serves the application with uWSGI instead of gunicorn. uWSGI speaks
HTTP, so the existing nginx configurations are unchanged. It also serves the static files, now collected into the image at build time.
2: Adds docker-compose.nginx-proxy.yml, with nginx-proxy and
acme-companion in place of the layersweb (nginx) and layerscertbot
containers: the certificate is obtained and renewed automatically, with no hand-edited nginx configuration. The existing compose files are unchanged.

Patch 1 can be taken on its own; patch 2 needs it for the static files.

This series is based on "docker-compose: run layersapp with an init process", sent separately, which isn't an RFC.

Changes in v2:
- 2: layersapp runs with "init: true", as in the other compose files after the patch above, and the compose file test covers docker-compose.nginx-proxy.yml too.

Tested: the test suite passes in the image; the test instance has run uWSGI behind nginx-proxy, with Let's Encrypt, since October.

Open questions:
- Should uWSGI replace gunicorn, or be an option?
- dockersetup.py does not know about the nginx-proxy setup; should it?
- The test instance also waits for the database and runs migrate and collectstatic when the container starts, instead of in dockersetup.py. I left that out; is it wanted?

Tim Orling (2):
Dockerfile: serve the application with uWSGI docker-compose.nginx-proxy.yml: add a production setup using
nginx-proxy

 .env.prod.proxy                |  7 +++
 Dockerfile                     | 15 ++++--
 README.md                      | 12 +++++
 docker-compose.nginx-proxy.yml | 93 ++++++++++++++++++++++++++++++++++
 tests/test_dockersetup.py      |  3 +-
 5 files changed, 124 insertions(+), 6 deletions(-)
 create mode 100644 .env.prod.proxy
 create mode 100644 docker-compose.nginx-proxy.yml

base-commit: 08e58cdece30019a6f9e5d14af16ebe20afa6177
prerequisite-patch-id: 4ad6ed99d0c42ce111a25d4731d953218337e7cc

Signature · 1 line
2.56.1
author
#1 lore raw

RFC PATCH v2 1/2 Dockerfile: serve the application with uWSGI

Replace gunicorn with uWSGI, keeping 4 workers and the 60 second request timeout. uWSGI serves HTTP, as gunicorn did, so the nginx configurations work unchanged. It also serves the static files, which are now collected into the image, for proxies that don't serve them, such as nginx-proxy.

AI-Generated: Claude Opus 5.5

---
 Dockerfile | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
Dockerfile +10 -5
index f4d19ab..35c49fc 100644
@@ -53,7 +53,7 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update \
 	&& locale-gen en_US.UTF-8 \
 	&& update-locale \
     && pip3 install --ignore-installed setuptools wheel \
-    && pip3 install gunicorn \
+    && pip3 install uwsgi \
     && pip3 install -r /requirements.txt \
     && apt-get purge -y autoconf g++ make python3-dev pkg-config libjpeg-dev \
 	&& apt-get autoremove -y \
@@ -68,14 +68,19 @@ COPY docker/updatelayers.sh ${APP_DIR}/updatelayers.sh
 COPY docker/migrate.sh ${APP_DIR}/migrate.sh
 COPY docker/connectivity_check.sh ${APP_DIR}/connectivity_check.sh
 
-RUN mkdir ${APP_DIR}/workdir \
+RUN mkdir ${APP_DIR}/workdir ${APP_DIR}/static \
 	&& adduser --system --uid=500 --home /home/layers layers \
-	&& chown -R layers ${APP_DIR}/workdir
+	&& chown -R layers ${APP_DIR}/workdir ${APP_DIR}/static
 USER layers
 
+# Collect the static files for uWSGI to serve (see below)
+RUN cd ${APP_DIR}/layerindex \
+	&& SECRET_KEY=collectstatic STATIC_ROOT=${APP_DIR}/static python3 manage.py collectstatic --noinput -v0
+
 # Always copy in .gitconfig and proxy helper script (they need editing to be active)
 COPY docker/.gitconfig /home/layers/.gitconfig
 COPY docker/git-proxy ${APP_DIR}/bin/git-proxy
 
-# Start Gunicorn
-CMD ["/bin/sh", "-c", "/usr/local/bin/gunicorn wsgi:application --workers=4 --bind=:5000 --timeout=60 --log-level=debug --chdir=${APP_DIR}/layerindex"]
+# Start uWSGI, serving HTTP so that it works behind any proxy. It also serves
+# the static files, for proxies that don't (e.g. nginx-proxy).
+CMD ["/bin/sh", "-c", "exec /usr/local/bin/uwsgi --http-socket :5000 --master --workers 4 --enable-threads --harakiri 60 --die-on-term --need-app --chdir ${APP_DIR}/layerindex --module wsgi:application --static-map /static=${APP_DIR}/static"]
Signature · 1 line
2.56.1
author
#2 lore raw

RFC PATCH v2 2/2 docker-compose.nginx-proxy.yml: add a production setup using nginx-proxy

In reply to Tim Orling

Add docker-compose.nginx-proxy.yml, which uses nginx-proxy and acme-companion in place of the layersweb and layerscertbot containers of docker-compose.prod.yml: the Let's Encrypt certificate is obtained and renewed automatically, without a hand-edited nginx configuration, and layersapp serves the static files. The host name and email address go in .env.prod.proxy.

AI-Generated: Claude Opus 5.5

---
 .env.prod.proxy                |  7 +++
 README.md                      | 12 +++++
 docker-compose.nginx-proxy.yml | 93 ++++++++++++++++++++++++++++++++++
 tests/test_dockersetup.py      |  3 +-
 4 files changed, 114 insertions(+), 1 deletion(-)
 create mode 100644 .env.prod.proxy
 create mode 100644 docker-compose.nginx-proxy.yml
.env.prod.proxy new file +7 -0
new file mode 100644
index 0000000..1350f27
@@ -0,0 +1,7 @@
+# For docker-compose.nginx-proxy.yml. Set real values in a local commit on the
+# server; never push them.
+# The site's host name, for nginx-proxy and for the certificate
+#VIRTUAL_HOST=<set this here>
+#LETSENCRYPT_HOST=<set this here>
+# The email address Let's Encrypt sends expiry notices to
+#LETSENCRYPT_EMAIL=<set this here>
README.md +12 -0
index a1f6087..c912034 100644
@@ -101,6 +101,18 @@ credentials, email server, `DEBUG`) from `.env.<context>.<service>` files, where
   docker compose -f docker-compose.prod.yml up -d --build
   ```
 
+- `docker-compose.nginx-proxy.yml` is a production setup that uses
+  [nginx-proxy](https://github.com/nginx-proxy/nginx-proxy) and
+  [acme-companion](https://github.com/nginx-proxy/acme-companion) in place of
+  the `layersweb` and `layerscertbot` containers: the certificate is obtained
+  and renewed automatically, and `layersapp` serves the static files. It uses
+  the `.env.prod.*` files, and `.env.prod.proxy` for the host name and the
+  email address for Let's Encrypt:
+
+  ```sh
+  docker compose -f docker-compose.nginx-proxy.yml up -d --build
+  ```
+
 The `.env*` files are excluded from the Docker images.
 
 Other settings the app and celery files can set:
docker-compose.nginx-proxy.yml new file +93 -0

93 lines not on this page · raw message

tests/test_dockersetup.py +2 -1
index 4d61ee7..d94ab05 100644
@@ -203,7 +203,8 @@ def test_compose_command(monkeypatch, result, expected):
 
 
 @pytest.mark.parametrize('compose_file', ['docker-compose.yml', 'docker-compose.prod.yml',
-                                          'docker-compose.staging.yml'])
+                                          'docker-compose.staging.yml',
+                                          'docker-compose.nginx-proxy.yml'])
 def test_compose_layersapp_has_init(compose_file):
     # [YOCTO #16515] in "docker compose run --rm layersapp ..." the script is
     # process 1; without an init process the bitbake servers started by
Signature · 1 line
2.56.1
#3 lore raw
In reply to Tim Orling
Quoted 3 lines · Tim Orling

This is an RFC: I run the layer index test instance I use for these patches this way, and would like to know if this is useful upstream, and in particular whether the production site would use it.

After some discussion with halstead and some additional research, I am dropping this series and moving to 'gunicorn' on my test bench(es).

Key takeaways:

* uWSGI is maintenance only since 2022 [1][2]:

"Note The project is in maintenance mode (only bugfixes and updates for
new languages apis). Do not expect quick answers on github issues and/or pull requests (sorry for that) A big thanks to all of the users and contributors since 2009."

* 'gunicorn' is recommended over 'granian' (and some other providers) for most users [2]. The article says to keep gunicorn with sync workers for a Django app like ours. Bigger gains come from the application:
caching, query optimisation and removing N+1 queries "typically yield 10× the improvement of switching application servers".

[1] https://uwsgi-docs.readthedocs.io/en/latest/
[2] https://www.deployhq.com/blog/python-application-servers-wsgi-vs-asgi-guide

Quoted 48 lines

1: Serves the application with uWSGI instead of gunicorn. uWSGI speaks
HTTP, so the existing nginx configurations are unchanged. It also serves the static files, now collected into the image at build time.
2: Adds docker-compose.nginx-proxy.yml, with nginx-proxy and
acme-companion in place of the layersweb (nginx) and layerscertbot
containers: the certificate is obtained and renewed automatically, with no hand-edited nginx configuration. The existing compose files are unchanged.

Patch 1 can be taken on its own; patch 2 needs it for the static files.

This series is based on "docker-compose: run layersapp with an init process", sent separately, which isn't an RFC.

Changes in v2:
- 2: layersapp runs with "init: true", as in the other compose files after the patch above, and the compose file test covers docker-compose.nginx-proxy.yml too.

Tested: the test suite passes in the image; the test instance has run uWSGI behind nginx-proxy, with Let's Encrypt, since October.

Open questions:
- Should uWSGI replace gunicorn, or be an option?
- dockersetup.py does not know about the nginx-proxy setup; should it?
- The test instance also waits for the database and runs migrate and collectstatic when the container starts, instead of in dockersetup.py. I left that out; is it wanted?

Tim Orling (2):
Dockerfile: serve the application with uWSGI docker-compose.nginx-proxy.yml: add a production setup using
nginx-proxy

 .env.prod.proxy                |  7 +++
 Dockerfile                     | 15 ++++--
 README.md                      | 12 +++++
 docker-compose.nginx-proxy.yml | 93 ++++++++++++++++++++++++++++++++++
 tests/test_dockersetup.py      |  3 +-
 5 files changed, 124 insertions(+), 6 deletions(-)
 create mode 100644 .env.prod.proxy
 create mode 100644 docker-compose.nginx-proxy.yml

base-commit: 08e58cdece30019a6f9e5d14af16ebe20afa6177
prerequisite-patch-id: 4ad6ed99d0c42ce111a25d4731d953218337e7cc