Linux – certbot in docker

Running certbot in docker container

create docker-compose.yml

#docker-compose.yml

version: "3.8"

services:
  certbot:
    image: certbot/certbot
    volumes:
      - ./etc-letsencrypt:/etc/letsencrypt
      - ./var-lib-letsencrypt:/var/lib/letsencrypt
      - ./var-log-letsencrypt:/var/log/letsencrypt

create required directories

mkdir etc-letsencrypt
mkdir var-lib-letsencrypt
mkdir var-log-letsencrypt
docker-compose run --name cbcontainer certbot certonly -d YOUR_DOMAIN --manual --preferred-challenges dns

first run

docker-compose run certbot certonly -d YOUR_DOMAIN --manual --preferred-challenges dns

Certificates will be saved in:

Certificate -> etc-letsencrypt/live/YOUR_DOMAIN/fullchain.pem
     Key    -> etc-letsencrypt/live/YOUR_DOMAIN/privkey.pem

Delete the container:

docker container rm cbcontainer

Renew cert

docker-compose run --name certbot certbot certonly --webroot -w /tmp/letsencrypt -d YOUR_DOMAIN
docker container rm certbot

Deploy cert example

cp -LRv YOUR_CERTBOT_DIR/etc-letsencrypt/live/* TARGET_DIR