I've been using the fairly popular nginx-proxy reverse proxy for Docker containers, created by Jason Wilder. It's a slick, super-simple method of putting many containers on a single host that all need to share HTTP/HTTPS ports. I am also a huge fan of the Let's Encrypt project. Free SSL certificates as long as you can prove that you are the domain's operator. This is really how it should work: In my book, forcing people to pay for SSL certificates is shitty and exploitative, especially considering how incredibly important encryption is these days. And incidentally, the behavior around self-signed certificates in browsers is stupid and broken.
So anyways, I played with the Let's Encrypt stuff late last year when they went into public beta. Best Christmas present ever! So the idea is pretty simple: You tell your Let's Encrypt client (probably best to use certbot) that you want a certificate, and it talks to the Let's Encrypt server, requesting a certificate from the CA. That causes Let's Encrypt to make a curl call to your domain, requesting a specific resource that certbot creates. That resource looks something like
http://www.example.com/.well-known/acme-challenge/g89SrgM4UAJGHiukm3GqQ3xMjTnpN-kZDYb27u4aTRW. That resource is just a regular file on disk. It has contents that look something like
DTe7mGGhLlML7Vlh4dyNTu97OiIrIIs7xd5O0Fpmlq8.TaRs2K47il2D0K9RjmOKOx7Neuu91FdEpLp2Wo4FcNI. As long as that resource matches what Let's Encrypt is looking for, you get a free SSL certificate! And they've built some magic into certbot so that it automagically installs that cert into your webserver if it's a common one (e.g. Apache or Nginx or something).
I wanted to use it on on my Dockerized web frontends, which use nginx-proxy. I had spotted a couple of issues on nginx-proxy's Github page which mentioned Let's Encrypt, but I hadn't yet tried to get this working with my nginx-proxy container. Getting that to work does not seem like a trivial task. Not having the spare minutes to get Let's Encrypt working in my infrastructure, I put it on the back burner and made a mental note to check in every once in a while. Well, I completely forgot about it until I got an email recently reminding me to renew one of my SSL certs. And I'm not paying $8.99 for something that should be free, so I knew it was time to check back in with Let's Encrypt being incorporated into the nginx-proxy project.
Enter nginx-proxy-letsencrypt-companion. This is a docker container that sits coupled to your nginx-proxy container, sharing its volumes and paying attention to containers spinning up that have
LETSENCRYPT_EMAIL environment variables set. The idea is that you start your nginx-proxy container, then start up this nginx-proxy-letsencrypt-companion container, and then start up your other containers that need Let's Encrypt certificates. The companion will request new Let's Encrypt certificates for containers that do not have current certificates and which also have those
LETSENCRYPT_* environment variables set.
So here are my notes for getting this going. I ended up adding
/usr/share/nginx/html as a data volume in my nginx-proxy container, and making a couple of the volumes
rw instead of
ro. Thus, my nginx-proxy run command looks something like this:
docker run -d \ --name="nginx-proxy" \ --restart="always" \ -p 80:80 \ -p 443:443 \ -v "/var/docker/nginx-proxy/htpasswd:/etc/nginx/htpasswd" \ -v "/var/docker/nginx-proxy/vhost.d:/etc/nginx/vhost.d" \ -v "/var/docker/nginx-proxy/certs:/etc/nginx/certs" \ -v "/var/run/docker.sock:/tmp/docker.sock" \ -v "/usr/share/nginx/html" \ jwilder/nginx-proxy
And the command for the brand new nginx-proxy-letsencrypt-companion container looks like this:
docker run -d \ --name="nginx-proxy-letsencrypt-companion" \ --restart="always" \ -v "/var/run/docker.sock:/var/run/docker.sock:ro" \ --volumes-from "nginx-proxy" \ jrcs/nginx-proxy-letsencrypt-companion
And finally, your individual containers will follow this pattern (note the environment variables mentioned above):
docker run -d \ --name="example.com" \ --restart="always" \ -e "VIRTUAL_HOST=example.com,www.example.com" \ -e "VIRTUAL_PORT=2368" \ -e "LETSENCRYPT_HOST=example.com,www.example.com" \ -e "LETSENCRYPT_EMAILfirstname.lastname@example.org" \ -v /var/docker/example.com/ghost:/var/lib/ghost \ ghost
And there you have it! Once you get your containers up -- in that order: nginx-proxy, nginx-proxy-letsencrypt-companion, and your web container -- give the Let's Encrypt a minute to phone home and get a call back, and you'll have a free SSL certificate and related miscellany there in
/var/docker/nginx-proxy/certs on your host. Also, the reason I created a data volume in the nginx-proxy container at
/usr/share/nginx/html instead of a bind-mount volume. I do this because the Let's Encrypt client in the companion container will continually update the authorization data, and I don't want to have to worry later about cleaning up a huge, sprawling directory full of those files, some of which would contain valid information, and many others containing invalid information. Of course, you do whatever you want, as long as its right for you. Good luck!