• Docker-compose with let's encrypt: TLS Challenge
    • Prerequisite
    • Setup
    • Explanation

    Docker-compose with let's encrypt: TLS Challenge

    This guide aim to demonstrate how to create a certificate with the let's encrypt TLS challenge to use https on a simple service exposed with Traefik.Please also read the basic example for details on how to expose such a service.

    Prerequisite

    For the TLS challenge you will need:

    • A publicly accessible host allowing connections on port 443 with docker & docker-compose installed.
    • A DNS record with the domain you want to expose pointing to this host.

    Setup

    • Create a docker-compose.yml on your remote server with the following content:
    1. version: "3.3"
    2. services:
    3. traefik:
    4. image: "traefik:v2.0.0-rc3"
    5. container_name: "traefik"
    6. command:
    7. #- "--log.level=DEBUG"
    8. - "--api.insecure=true"
    9. - "--providers.docker=true"
    10. - "--providers.docker.exposedbydefault=false"
    11. - "--entrypoints.websecure.address=:443"
    12. - "--certificatesresolvers.mytlschallenge.acme.tlschallenge=true"
    13. #- "--certificatesresolvers.mytlschallenge.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
    14. - "[email protected]omain.com"
    15. - "--certificatesresolvers.mytlschallenge.acme.storage=/letsencrypt/acme.json"
    16. ports:
    17. - "443:443"
    18. - "8080:8080"
    19. volumes:
    20. - "./letsencrypt:/letsencrypt"
    21. - "/var/run/docker.sock:/var/run/docker.sock:ro"
    22. whoami:
    23. image: "containous/whoami"
    24. container_name: "simple-service"
    25. labels:
    26. - "traefik.enable=true"
    27. - "traefik.http.routers.whoami.rule=Host(`whoami.mydomain.com`)"
    28. - "traefik.http.routers.whoami.entrypoints=websecure"
    29. - "traefik.http.routers.whoami.tls.certresolver=mytlschallenge"
    • Replace [email protected] by your own email within the certificatesresolvers.mytlschallenge.acme.email command line argument of the traefik service.
    • Replace whoami.mydomain.com by your own domain within the traefik.http.routers.whoami.rule label of the whoami service.
    • Optionally uncomment the following lines if you want to test/debug:
    1. #- "--log.level=DEBUG"
    2. #- "--certificatesresolvers.mytlschallenge.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
    • Run docker-compose up -d within the folder where you created the previous file.

    • Wait a bit and visit https://your_own_domain to confirm everything went fine.

    Note

    If you uncommented the acme.caserver line, you will get an SSL error, but if you display the certificate and see it was emitted by Fake LE Intermediate X1 then it means all is good.(It is the staging environment intermediate certificate used by let's encrypt).You can now safely comment the acme.caserver line, remove the letsencrypt/acme.json file and restart Traefik to issue a valid certificate.

    Explanation

    What changed between the basic example:

    • We replace the web entry point by one for the https traffic:
    1. command:
    2. # Traefik will listen to incoming request on the port 443 (https)
    3. - "--entrypoints.websecure.address=:443"
    4. ports:
    5. - "443:443"
    • We configure the Https let's encrypt challenge:
    1. command:
    2. # Enable a tls challenge named "mytlschallenge"
    3. - "--certificatesresolvers.mytlschallenge.acme.tlschallenge=true"
    • We add a volume to store our certificates:
    1. volumes:
    2. # Create a letsencrypt dir within the folder where the docker-compose file is
    3. - "./letsencrypt:/letsencrypt"
    4. command:
    5. # Tell to store the certificate on a path under our volume
    6. - "--certificatesresolvers.mytlschallenge.acme.storage=/letsencrypt/acme.json"
    • We configure the whoami service to tell Traefik to use the certificate resolver named mytlschallenge we just configured:
    1. labels:
    2. # Uses the Host rule to define which certificate to issue
    3. - "traefik.http.routers.whoami.tls.certresolver=mytlschallenge"