Multi domain Nginx with automatic Letsencrypt certificate
What was the problem? I had a SWARM cluster with just one node and a Nginx server installed bare metal with all the domain config files in a folder. In this scenario, every new application I had to deploy on the cluster, map a different port and tell baremetal Nginx/CertBot to issue the certificate and proxy the requests.
Issues I neede to solve
- Docker swarm single node must be converted do multi node
- New apps should have Lets Encrypt certificate issued automatically
- Needed to redirect HTTP->HTTPS
- Every container should have one port
- If a container is deployed on other node, the port would be mapped on the other host so Nginx in cluster head won´t find the service
As is diagram
To be diagram
Solution: use the nginx-proxy + acme-companion deployment
Conventions
- I used /opt/docker as my volumes mount.
- I used docker-compose.servicename.yaml as the docker-compose file name (I don´t want one folder per app, just like that :))
- This article does not cover database or applications (WordPress, Odoo, Miniflux) deployment so, database and other stuff must work first. I advise you to map host port before deploying to ensure the applications is working without certificate
Docker-compose files
docker-compose.wordpress.yaml
version: "3"services: teampass: image: wordpress environment: VIRTUAL_HOST: wordpress.xnv.io LETSENCRYPT_HOST: wordpress.xnv.io LETSENCRYPT_EMAIL: andre@xnv.io networks: - common_backend volumes: - /opt/docker/miniflux:/var/www/htmlnetworks: common_backend: external: true
version: "3"services: teampass: image: miniflux environment: VIRTUAL_HOST: miniflux.xnv.io LETSENCRYPT_HOST: miniflux.xnv.io LETSENCRYPT_EMAIL: andre@xnv.io networks: - common_backend volumes: - /opt/docker/miniflux:/var/www/htmlnetworks: common_backend: external: true
version: "3"services: teampass: image: odoo environment: VIRTUAL_HOST: odoo.xnv.io LETSENCRYPT_HOST: odoo.xnv.io LETSENCRYPT_EMAIL: andre@xnv.io VIRTUAL_PORT: 8069 networks: - common_backend volumes: - /opt/docker/wordpress:/var/www/htmlnetworks: common_backend: external: true
docker-compose.common.yaml
version: '3.3'services: nginx-proxy: image: nginxproxy/nginx-proxy ports: - '80:80' - '443:443' deploy: placement: constraints: - node.hostname == megazord volumes: - 'certs:/etc/nginx/certs:rw' - 'vhost:/etc/nginx/vhost.d:rw' - 'html:/usr/share/nginx/html' - '/var/run/docker.sock:/tmp/docker.sock:ro' labels: - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy" networks: - common_backend acme-companion: image: nginxproxy/acme-companion volumes: - '/var/run/docker.sock:/var/run/docker.sock:ro' - 'acme:/etc/acme.sh' - 'certs:/etc/nginx/certs:rw' - 'vhost:/etc/nginx/vhost.d:rw' - 'html:/usr/share/nginx/html' deploy: placement: constraints: - node.hostname == megazord environment: - DEFAULT_EMAIL=andre@xnv.io networks: - common_backendvolumes: certs: vhost: html: acme:networks: backend: driver: overlay
Some things to pay attention:
Have a SWARM cluster already set. If you don´t, I’ll write a post with the step-by-step for this sometime.
Network must be the same for Acme Companion, Nginx Proxy and your apps
Once the nginx-proxy will call your service by the name (e.g. http://teampass) in order to respond your client, you must determine these three variables
The “common” name is used here to say that all services that is common to all stacks, is declared on docker-compose.common.yaml file (databases, redis and other services too). Deploy this first to ensure creation of “common_backend” network
LETSENCRYPT_HOST: wordpress.xnv.io #this is the same name you will configure in our DNS LETSENCRYPT_EMAIL: andre@xnv.io #some emmail VIRTUAL_PORT: 8080 #the port that the container exposes inside the SWARM cluster. If the port is 80, you can hide this variable
The volumes in nginx stack will be shared between nginx-proxy and nginx-acme_companion. Because of this, you should declare the volumes in the top level of yaml
That said, lets deploy it!
docker stack deploy -c docker-compose.common.yaml commondocker stack deploy -c docker-compose.wordpress.yaml wordpressdocker stack deploy -c docker-compose.miniflux.yaml minifluxdocker stack deploy -c docker-compose.odoo.yaml odoo
THE BLOG
News, lessons, and content from our companies and projects.
41% of small businesses that employ people are operated by women.
We’ve been talking to several startups in the past two weeks! This is a curated list of the top 5 based on the analysis made by our models using the data we collected. This is as fresh as ...
Porto Seguro Challenge – 2nd Place Solution
We are pleased to announce that we got second place in the Porto Seguro Challenge, a competition organized by the largest insurance company in Brazil. Porto Seguro challenged us to build an ...
Predicting Reading Level of Texts – A Kaggle NLP Competition
Introduction: One of the main fields of AI is Natural Language Processing and its applications in the real world. Here on Amalgam.ai we are building different models to solve some of the problems ...
Porto Seguro Challenge
Introduction: In the modern world the competition for marketing space is fierce, nowadays every company that wants the slight advantage needs AI to select the best customers and increase the ROI ...
Sales Development Representative
At Exponential Ventures, we’re working to solve big problems with exponential technologies such as Artificial Intelligence, Quantum Computing, Digital Fabrication, Human-Machine ...
Exponential Hiring Process
The hiring process is a fundamental part of any company, it is the first contact of the professional with the culture and a great display of how things work internally. At Exponential Ventures it ...
Exponential Ventures annonce l’acquisition de PyJobs, FrontJobs et RecrutaDev
Fondé en 2017, PyJobs est devenu l’un des sites d’emploi les plus populaires du Brésil pour la communauté Python. Malgré sa croissance agressive au cours de la dernière année, ...
Exponential Ventures announces the acquisition of PyJobs, FrontJobs, and RecrutaDev
Founded in 2017, PyJobs has become one of Brazil’s most popular job boards for the Python community. Despite its aggressive growth in the past year, PyJobs retained its community-oriented ...
Sales Executive
At Exponential Ventures, we’re working to solve big problems with exponential technologies such as Artificial Intelligence, Quantum Computing, Digital Fabrication, Human-Machine ...
What is a Startup Studio?
Spoiler: it is NOT an Incubator or Accelerator I have probably interviewed a few hundred professionals in my career as an Entrepreneur. After breaking the ice, one of the first things I do is ask ...
Social Media
At Exponential Ventures, we’re working to solve big problems with exponential technologies such as Artificial Intelligence, Quantum Computing, Digital Fabrication, Human-Machine ...
Hunting for Unicorns
Everybody loves unicorns, right? But perhaps no one loves them more than tech companies. When hiring for a professional, we have an ideal vision of who we are looking for. A professional with X ...
Stay In The Loop!
Receive updates and news about XNV and our child companies. Don't worry, we don't SPAM. Ever.