Django over HTTPS in local Docker
Edit. This can be done in a much simpler manner with Caddy
sudo caddy reverse-proxy --from my-site.test --to localhost:8000
h/t Ben Dickinson who also notes
any subdomain of
localhostresolves tolocalhost, which save lots of time messing around with hosts files
which is so true, unless you prefer .test or .local instead.
I needed to test a local build over HTTPS due SSO requirements in the past, and Referrer-Policy more recently.
The project(s) have the following layout:
.
└── Project root/
│ ├── docker/
│ │ ├── Procfile
│ │ └── other-docker-related-files
│ ├── the_project/
│ ├── Dockerfile
│ └── docker-compose.yaml
│ ...
Where Procfile is for honcho. A Procfile specifies
commands that are executed on startup by an app that supports it and uses a <process type>: <command> format:
web: python manage.py runserver 0.0.0.0:8000
On my host:
- Install mkcert
- Generate a new self-signed certificate
cd <path/to/project/>docker
mkcert -cert-file dev-cert.pem -key-file dev-key.pem localhost 127.0.0.1 my-project.local
Run
mkcert -installfrom thedocker/directory which contains the self-signed certificate (dev-cert.pemanddev-key.pem)(optional) Create a
Procfile_httpsfile withweb: python manage.py runserver_plus 0.0.0.0:8000 --cert-file docker/dev-cert.pem --key-file docker/dev-key.pem --keep-meta-shutdownIn your web container:
- if you use honcho and created
Procfile_https, usehoncho_https runinstead ofhoncho run - if running
runserverdirectly, then runpython manage.py runserver_plus 0.0.0.0:8000 --cert-file docker/dev-cert.pem --key-file docker/dev-key.pem --keep-meta-shutdown
- if you use honcho and created
Access the site at
https://my-site.test:8000Note: add this domain to your local
hostsfile, if it’s not there already