Table of contents
![blog cover](https://namvo.net/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBLdz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--62d553ddc6d89c43a494f1e829d5763427c0bbac/Screenshot%202024-08-15%20at%2021.05.10.jpg)
How to Manage and Renew SSL Certificates Using Docker Compose
Software Engineer
Software Engineer
Docker
Docker
Securing your website with SSL certificates is essential to protect data transmitted between the server and client. Managing and renewing SSL certificates can be a cumbersome task, but Docker simplifies this process. In this blog post, we will demonstrate how to manage and renew SSL certificates using Docker Compose.
Introduction
Let's Encrypt
Let's Encrypt is a free, automated, and open certificate authority (CA) that provides SSL/TLS certificates to enable HTTPS on websites. Its mission is to create a more secure and privacy-respecting Web by promoting the widespread adoption of HTTPS. Certificates issued by Let's Encrypt are valid for 90 days, after which they must be renewed.
Certbot
Certbot is a tool developed by the Electronic Frontier Foundation (EFF) to automate the process of obtaining and renewing Let's Encrypt SSL certificates. It simplifies the certificate issuance and renewal process, making it accessible even to those with limited technical knowledge.
Prerequisites
Before you start, ensure you have:
- Docker installed on your system. Follow the official installation guide if necessary.
- Docker Compose installed. Follow the installation guide if needed.
- Basic understanding of Docker and Docker Compose.
Step 1: Create a Docker Compose File
Create a file named docker-compose-cert.yml with the following content:
// language: yaml version: "3.9" services: letsencrypt: container_name: "certbot-service" image: certbot/certbot command: sh -c "certbot certonly --webroot -w /tmp/acme_challenge -d your-app.com --text --agree-tos --email admin@gmail.com --rsa-key-size 4096 --verbose --keep-until-expiring --preferred-challenges=http" entrypoint: "" volumes: - /etc/letsencrypt:/etc/letsencrypt - /tmp/acme_challenge:/tmp/acme_challenge environment: - TERM=xterm
Make sure to replace your-app.com with your domain and admin@gmail.com with your email address.
Step 2: Configure Your Web Server
Ensure that your web server serves the /.well-known/acme-challenge/ directory from the /tmp/acme_challenge path. This is necessary for the webroot method used by Certbot to verify domain ownership.
If you use NGINX, add the following location block to your Nginx configuration:
// language: bash server { listen 80; server_name your-app.com; location /.well-known/acme-challenge/ { root /tmp/acme_challenge; } }
Step 3: Run Docker Compose
Navigate to the directory containing your docker-compose-cert.yml file and run the following command
// language: bash docker-compose -f docker-compose-cert.yml up --build nginx -s reload
After renewing the certificates, you need to reload your web server to apply the changes. If you use NGINX, run the below command
// language: bash nginx -s reload
You can verify the new certificate in your browser
Step 4: Schedule Automatic Renewals
To keep your SSL certificates up-to-date, you should renew them periodically. Since Let's Encrypt certificates are valid for 90 days, it's a good practice to renew them every ~80 days. You can automate this process using a cron job.
Open the crontab editor by running:
Open the crontab editor by running:
// language: bash crontab -e
Add a new line to schedule the renewal process. The following example schedules the renewal to run every 80 days at 2 AM:
// language: bash 0 2 */80 * * docker-compose -f /path/to/docker-compose-cert.yml run --rm letsencrypt && nginx -s reload
The --rm option is used when running a Docker container. It ensures that the container is automatically removed after it exits. This is useful for containers that are used for one-time tasks, such as renewing SSL certificates, to avoid cluttering your system with stopped containers.
Make sure to replace /path/to/docker-compose-cert.yml with the actual path to your Docker Compose file.
Make sure to replace /path/to/docker-compose-cert.yml with the actual path to your Docker Compose file.
Conclusion
By following these steps, you can efficiently manage and renew SSL certificates using Docker. This setup ensures that your SSL certificates are always up-to-date, enhancing the security and reliability of your website without requiring manual intervention.
Created at
2024-05-26 15:44:03 +0700
Related blogs
Beware of html_safe in Rails !!!
Modern web frameworks provide a secure way to develop new applications. However, if we do not understand it well, it still leads to security issues.ht...
Software Engineer
Software Engineer
2024-08-26 19:27:04 +0700
Integer and String Enum in Rails
TL;DR
For General Use: I would lean towards using strings for enums due to their readability and maintainability benefits.For Performance-Critical Ap...
Software Engineer
Software Engineer
2024-09-05 23:53:58 +0700