DANIEL
STOKES

Easy ssl certificates for docker with letsencrypt

March 31, 2021 at 2:16 PM

letsencrypt offers free ssl certificates to all and its super easy to setup.

Having a ssl certificate is so crucial these days not only for security but because web browsers may prevent your page from being loaded without one. If you're using docker then here is a really simple way of getting ssl setup and with the added bonus of easy subdomain setup.

This is setup using nginx-proxy and the docker-letsencrypt-nginx-proxy-companion

Below you can see an example docker-compose file to set this up.

docker-compose.yml
version: "3.7"

services:
  reverse-proxy:
      image: "jwilder/nginx-proxy:latest"
      container_name: "reverse-proxy"
      volumes:
        - "html:/usr/share/nginx/html"
        - "dhparam:/etc/nginx/dhparam"
        - "vhost:/etc/nginx/vhost.d"
        - "certs:/etc/nginx/certs"
        - "/run/docker.sock:/tmp/docker.sock:ro"
      restart: "always"
      environment:
        - HTTPS_METHOD=redirect
      networks: 
        - "net"
      ports:
        - "80:80"
        - "443:443"

  letsencrypt:
    image: "jrcs/letsencrypt-nginx-proxy-companion:latest"
    container_name: "letsencrypt-helper"
    volumes:
      - "html:/usr/share/nginx/html"
      - "dhparam:/etc/nginx/dhparam"
      - "vhost:/etc/nginx/vhost.d"
      - "certs:/etc/nginx/certs"
      - "/run/docker.sock:/var/run/docker.sock:ro"
    environment:
      NGINX_PROXY_CONTAINER: "reverse-proxy"
      DEFAULT_EMAIL: "myemail@test.com"
    restart: "always"
    depends_on:
      - "reverse-proxy"
    networks: 
      - "net"

volumes:
  certs:
  html:
  vhost:
  dhparam:

networks:
  net:
    external: true
Dockerfile
environment:
      - VIRTUAL_HOST=example.com
      - LETSENCRYPT_HOST=example.com
      - VIRTUAL_PORT=443
      - HTTPS_METHOD=redirect
networks: 	
      - "net"
© Daniel Stokes 2024