Skip to contentSkip to conversation

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

RFC PATCH Superseded Tim Orling 1 participant 3 messages Nov 14

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tim Orling

author
#0 lore raw

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.

Tested: the test suite passes in the image (75 passed, 1 skipped); uWSGI starts, serves static files and stops cleanly on SIGTERM; and the nginx-proxy setup serves pages and static files over HTTP locally. 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 | 90 ++++++++++++++++++++++++++++++++++
 4 files changed, 119 insertions(+), 5 deletions(-)
 create mode 100644 .env.prod.proxy
 create mode 100644 docker-compose.nginx-proxy.yml

base-commit: 08e58cdece30019a6f9e5d14af16ebe20afa6177

Signature · 1 line
2.56.0
author
#1 lore raw

RFC PATCH 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.0
author
#2 lore raw

RFC PATCH 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 | 90 ++++++++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+)
 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 +90 -0

90 lines not on this page · raw message

Signature · 1 line
2.56.0