I'm trying to add automatic TLS/SSL termination to an Nginx in a docker-compose deployed through the docker-machine (DigitalOcean).
I found a few nice resources [humankode/how-to-set-up..., medium/nginx-and-lets-encrypt...] on how to do it through the docker-compose but they both are saying from the perspective of being on the server. I really want to avoid that. I'd love to do it locally, on my laptop, bundle it all together, and send it off. Or, even do it remotely without doing any ssh.
A few attempts failed but it feels like they were close. The main obstacle seems to be with files/volumes. Following the medium/nginx-and-lets-encrytp... guide I'm having problems with saving OpenSSL privkey.pem. The other tutorial (humankode), to me knowledge, does everything on the server and that's where the volumes live.
My latest attempt was to set up certificates on the machine through the DigitalOcean tutorial and try to include these through in my docker-compose build. No luck.
There were many modifications but my settings are similar to:
docker-compose.yml
version: '3.7'
services:
nginx:
image: nginx:1.15.9-alpine
container_name: nginx
build:
context: ./nginx
dockerfile: Dockerfile
restart: always
volumes:
- /etc/letsencrypt
- /var/www/certbot
ports:
- "80:80"
- "443:443"
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
certbot:
image: certbot/certbot
restart: unless-stopped
volumes:
- "/etc/letsencrypt"
- "/var/www/certbot"
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
nginx/Dockerfile
FROM nginx:1.15.9-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY prod.conf /etc/nginx/conf.d/
nginx/conf.d
# PRODUCTION
server {
listen 80;
listen [::]:80;
server_name example.site;
location ~ /.well-known/acme-challenge {
allow all;
root /usr/share/nginx/html;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.site;
ssl_certificate /etc/letsencrypt/live/example.site/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.site/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;