🔨 Infrastructure
How to Create HTTPS in Docker Env

How to Create HTTPS in Docker Environment

In this article, we will learn how to create HTTPS in a Docker environment. We will use a self-signed certificate to create HTTPS in a Docker environment. We will create a self-signed certificate and use it to create HTTPS in a Docker environment.

Prerequisites

  • Docker installed on your system
  • Basic knowledge of Docker
  • Basic knowledge of the command line

Step 1: Add A Record to Cloudflare

First, you need to add an A record to Cloudflare. You can add an A record to Cloudflare by following the steps below:

Cloudflare

Step 2: Modify docker compose file

Next, you need to modify the docker-compose file. You can modify the docker-compose file by following the steps below:

Docker Compose

Step 3: Modify & Run ./init-letsencrypt.sh script

Next, you need to modify the ./init-letsencrypt.sh script.

init-letsencrypt

./init-letsencrypt.sh

This script will create a self-signed certificate and use it to create HTTPS in a Docker environment.

Step 4: Add HTTPS to Nginx Configuration

Next, you need to add HTTPS to the Nginx configuration. You can add HTTPS to the Nginx configuration by following the steps below:

server {
    listen 80;
    server_name new-domain.fyfirman.com;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }    
}

server {
    listen 443;
    server_name new-domain.fyfirman.com;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://uptime-kuma:3001";
    }

    ssl_certificate /etc/letsencrypt/live/new-domain.fyfirman.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/new-domain.fyfirman.com/privkey.pem;
}

Step 5: Restart Nginx

Next, you need to restart Nginx. You can restart Nginx by following the steps below:

sudo docker exec nginx nginx -s reload

Reference

https://pentacent.medium.com/nginx-and-lets-encrypt-with-docker-in-less-than-5-minutes-b4b8a60d3a71 (opens in a new tab)