HTTPS certificates by default for your Docker containers

Lucas Werner
2 min readFeb 8, 2021
Photo by NeONBRAND on Unsplash

You built your project, used Docker and docker-compose to make it scalable. Now it’s time to publish your project. You probably know at this point that HTTPS is a must these days, but also a pain to set it up if you have multiple services or even if you’ve never done that before.

Wouldn’t be nice if you could have this part also automated? Let me show you how. Let’s assume that you already have a server with Docker and docker-compose installed and your domain correctly points to it. How to setup DigitalOcean to point to a GoDaddy domain?

Let’s keep it simple. We only have a web server that needs to point to “domain.com”.

This is how your docker-compose file will look like:

We’ve added 2 new services that will take care of our certificates automatically (awesome):

Now the only step left is to setup an .env file (we could use replace the variables directly inside the docker-compose file ). So in the same directory where you have your docker-compose file, create an “.env” file with the following content:

These containers will detect the port that is being exposed on each of the services we have, in our case port is 3000 for the web server and it will proxy it to port 443(HTTPS) for the domain specified in VIRTUAL_HOST. They will automatically get the certificates from Let’s Encrypt and you will receive an email when they’re about to expire, but don’t worry, it get’s renewed automatically!

Both VIRTUAL_HOST and LETSENCRYPT_HOST must be the same.

Now just start your docker-compose file and that’s it! I use this on every project to get rid of the configuration of the domains.

Hope you find it useful. Let me know in the comments if you have any issue.

--

--

Lucas Werner

I am a computer engineer. Currently working with web technologies at Oracle. Passionate about AI and data science.