Install nginx and lets encrypt with docker


This article is based on the fact that you have docks installed. On the following link I’ve specified how to install docker: Install docker.
It is also based on an article from pattonWebs

Let’s encrypt allows creation of free ssl certificate for your site. This will make the page safe and get a url such as

I start by creating a folder for which nginx.tmpl file should be located, which nginx uses to configure each site, via nginx reverse proxy. With a reverse proxy, nginx understands what it should show when going to a url. For example, in dns settings for a domain, we point to the server’s ipaddress. The server can have multiple websites and how does the server know which page to display? Well, with the help of a reverse proxy, the server understands it. Thus shows a page and shows another page, though they are on the same server and in different docker containers. Now, my root user is peter, do not forget to change to your correct root user or another user on your Ubuntu server.

sudo mkdir -pv /home/peter/docker/volumes/proxy/templates/

Go to the template folder and create a nginx.tmpl file
cd /home/peter/docker/volumes/proxy/templates/
sudo nano nginx.tmpl

Then paste this code: nginx.tmpl

Create docker-compose file

Start by creating your own nginx docker network where all web sites will be located, so let’s encrypt detects when a new site is added and can create certificates.
docker network create nginx-proxy

Cd’a to the docker folder that was created earlier and create a docker-compose.yml file
cd /home/peter/docker/
sudo nano docker-compose.yml

Then paste this code for nginx with let’s encrypt: docker-compose.yml

Open ports

Then we also have to open ports in the firewall.
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Run docker-compose file

The last step is to run the nginx server and you run docker-compose.yml through the following command.
sudo docker-compose up

You can stay at that display and start a new ssh session with the server, if you are going to add a website, as we do in the next step of this tutorial. This is because in that display that comes, you can follow prints when creating the certificates etc.

Leave a Reply

Your email address will not be published. Required fields are marked *