Skip to content

Instantly share code, notes, and snippets.

@davidwtbuxton
Created April 24, 2025 08:18
Show Gist options
  • Save davidwtbuxton/746b4c919097072527e816dacb094e4e to your computer and use it in GitHub Desktop.
Save davidwtbuxton/746b4c919097072527e816dacb094e4e to your computer and use it in GitHub Desktop.
Docker compose with an nginx proxy for a Python app
# Nginx will listen on localhost:8080, serving static assets and doing gzip compression.
services:
app:
image: python:3.13-alpine
ports: ["8000:8000"]
command: ["python", "-m", "http.server"]
nginx:
build:
dockerfile: nginx.dockerfile
ports: ["8080:8080"]
environment:
PORT: "8080"
APP_PORT: "8000"
APP_HOST: "app"
# Nginx server configuration.
server {
listen ${PORT};
gzip on;
gzip_types
application/atom+xml
application/javascript
application/json
application/rss+xml
application/vnd.ms-excel
application/xml
image/svg+xml
image/jpeg
image/gif
image/png
image/webp
text/css
text/html
text/plain
text/xml
;
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ @app;
}
location @app {
proxy_pass http://${APP_HOST}:${APP_PORT};
}
}
# A custom nginx image including our static assets. This (mostly) works when running
# in Cloud Run, with $PORT usually provided as "8000", but with sidecar containers
# listening on 127.0.0.1 (which is different to Docker compose, where services have
# their own service name on a shared network).
FROM nginx:1.28-alpine
ENV APP_PORT=8000
ENV APP_HOST=127.0.0.1
ENV PORT=80
RUN rm /usr/share/nginx/html/index.html
COPY nginx-default.conf.template /etc/nginx/templates/default.conf.template
COPY static /usr/share/nginx/html/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment