Certificados Letsencrypt para tu servidor mediante docker
2019-08-19

En el articulo de hoy vamos a ver como utilizar un contenedor docker para generar y actualizar de forma automática los certificados que nos proporciona Letsencript de forma gratuita para nuestros nombres de subdominios obtenidos en Duckdns y que podemos utilizar para diferentes servicios en nuestro servidor mediante un proxy inverso, como veremos en próximos artículos.
Instalación
Voy a utilizar la imagen que proporciona Linuxserver en el hub de Docker, en la seccion Dockers, buscamos por letsencrypt

Una vez seleccionado si pulsamos sobre el icono de información,

Nos llevará a la página web de la imagen, donde podemos ver las tags de las arquitecturas soportadas, en mi caso, para mi placa, pondró arm64 como tag y pulso en el botón Comenzar


Abrir puertos en el router
Para poder obtener o renovar los certificado es necesario abrir los puertos 443 y 80 del router y redirigirlos a los puertos de escucha del docker, en mi caso como los purtos 80 y 443 los utilizo para otros servicios, redirigire los puertos al 1443 y 180 del docker.
Configuración
Cuando termine la descarga de la imagen, seleccionamos la imagen y pulsamos en el botón Ejecutar imagen

Ahora pasamos a configurar el contenedor

- 1: Nombre que le vamos a dar al contenedor
- 2: always, si queremos que se inicie de forma automatice
- 3: bridge, ya que vamos realiar NAT en el puerto 80 y 443
- 4: nombre host del servidor (ngnix) que corre en la imagen
- 5: vamos a indicarle al docker que todas las peticiones que lleguen por el puerto 1443 (https) y 180 (http), tendremos que hacer NAT desde el router, los redirija a los puertos 443 y 80, respectivamente. del contenedor

- 6: http
- 7: zona horaria que corresponda, en mi caso Europe/Madrid
- 8: nombre del subdominio de duckdns del certificado que vayas a crear/renovar, si utilizas más de un subdominio, deben de ir separados por comas y sin espacios, por ejemplo subdominio1,subdominio2,subdominio3 ( /cuidado no es subdominio1.duckdns.org, solo el nombre de subdominio / )
- 9: GID del usuario que ejecuta el docker (abre una terminal en el servidor y ejecuta el comando id)
- 10: pondremos el token de nuestra cuenta,aquí te explico como obtenerlo.
- 11: el id del usuario que ejecuta el docker.
- 12: duckdns.org
- 13: email para poder recibir las notificaciones desde Letsencript, por ejemplo cuando vaya a caducar el certificado
- 14: true

- 15: Es conveniente que te crees una carpeta compartida donde almacenar los certificados, asi en un futuro sera mas fácil acceder a ellos
- 16: –cap-add=NET-ADMIN, creamos una nueva interfaz de red para el docker
Para finalizar pulsamos en el botón Salvar
Podemos mirar el log para comprobar que ha arrancado correctamente, bien abriendo una terminal
[email protected]:~ $ sudo docker logs -f letsencrypt
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] 10-adduser: executing...
-------------------------------------
_ ()
| | ___ _ __
| | / __| | | / \
| | \__ \ | | | () |
|_| |___/ |_| \__/
Brought to you by linuxserver.io
We gratefully accept donations at:
https://www.linuxserver.io/donate/
-------------------------------------
GID/UID
-------------------------------------
User uid: 1000
User gid: 100
-------------------------------------
[cont-init.d] 10-adduser: exited 0.
[cont-init.d] 20-config: executing...
[cont-init.d] 20-config: exited 0.
[cont-init.d] 30-keygen: executing...
generating self-signed keys in /config/keys, you can replace these with your own keys if required
Generating a RSA private key
.+++++
............................................................................................+++++
writing new private key to '/config/keys/cert.key'
-----
[cont-init.d] 30-keygen: exited 0.
[cont-init.d] 50-config: executing...
Variables set:
PUID=1000
PGID=100
TZ=Europe/Madrid
URL=dominio.duckdns.org
SUBDOMAINS=
EXTRA_DOMAINS=
ONLY_SUBDOMAINS=false
DHLEVEL=2048
VALIDATION=duckdns
DNSPLUGIN=
EMAIL=[email protected]
STAGING=
Created donoteditthisfile.conf
Creating DH parameters for additional security. This may take a very long time. There will be another message once this process is completed
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
.....+......................................................+......................................+...................+.........+.....................................................................................+..........................................................................+......................................+.....................................................................................+.................................................................................................................+..+..................+............................................................................................................+..............................................................................................................................+.............+..+........................................................................................................................................................................................................+............................................................+.........................................+................................................................+........+.+....+.......................................................................................................................................+..........................................+...................................+........................+........................+.......................................................................+...........................................................................................................................................+......................+........................................................................................................................................................+.............................................................................+.....................................................................................................................................................................................................++*++*++*++*
DH parameters successfully created - 2048 bits
No subdomains defined
E-mail address entered: [email protected]
duckdns validation is selected
the resulting certificate will only cover the subdomains due to a limitation of duckdns, so it is advised to set the root location to use www.subdomain.duckdns.org
Generating new certificate
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for dominio.duckdns.org
Output from duckdns-txt:
OKsleeping 60
Error output from duckdns-txt:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 2 0 2 0 0 2 0 --:--:-- --:--:-- --:--:-- 2
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/dominio.duckdns.org/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/dominio.duckdns.org/privkey.pem
Your cert will expire on 2019-05-03. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
New certificate generated; starting nginx
[cont-init.d] 50-config: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
Server ready

Comprobación
Ahora deberías poder acceder a tu nombre de subdominio

Podemos comprobar la fortaleza de nuesrtro sitios desde https://www.ssllabs.com/ssltest/
Desde la terminal
Si no utilizas OpenMediaVault o si lo utilizas y prefieres crear el contenedor mediante la terminal, te dejo el comando completo:
docker create \
--name=letsencrypt \
--cap-add=NET_ADMIN \
-e PUID=1000 \
-e PGID=100 \
-e TZ=Europe/Madrid \
-e URL=duckdns.org \
-e SUBDOMAINS=dominio1,dominio2,dominio3 \
-e VALIDATION=http \
-e DNSPLUGIN=cloudflare \
-e DUCKDNSTOKEN=token de duckdns \
-e EMAIL=[email protected] \
-e DHLEVEL=2048 \
-e ONLY_SUBDOMAINS=true \
-p 1443:443 \
-p 180:80 \
-v /mnt/datos/Docker/Config/letsencrypt/:/config \
--restart always \
linuxserver/letsencrypt:arm64v8-latest
Cambia la etiqueta (tag) arm64v8-latest por la arquitectura de tu equipo
Espero que te haya gustado, pasa un buen día.