Skip to content

Instantly share code, notes, and snippets.

@k8scat
Last active April 9, 2021 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k8scat/f28171bda1e1373d42f49a28f344e6ec to your computer and use it in GitHub Desktop.
Save k8scat/f28171bda1e1373d42f49a28f344e6ec to your computer and use it in GitHub Desktop.
Generate letsencrypt certs via certbot in docker with pre_hook and post_hook.
#!/bin/bash
set -e
domain=$1
email=$2
pre_hook=$3
post_hook=$4
if [[ -z "${domain}" || -z "${email}" ]]; then
echo "usage: $0 <domain> <email> [pre_hook] [post_hook]"
exit 1
fi
# Command to be run in a shell before obtaining any certificates.
if [[ -n "${pre_hook}" ]]; then
/bin/bash -xc "${pre_hook}"
fi
# About options: docker run --rm certbot/certbot:latest -h
volume_dir="$(pwd)/${domain}"
if [[ -d "${volume_dir}" ]]; then
rm -rf ${volume_dir}
fi
docker run --rm \
--name certbot \
-p 80:80 \
-v "${volume_dir}:/etc/letsencrypt/archive/${domain}" \
certbot/certbot:latest \
certonly \
--standalone \
--agree-tos \
-n -m ${email} -d ${domain}
# Command to be run in a shell after attempting to obtain certificates
if [[ -n "${post_hook}" ]]; then
/bin/bash -xc "${post_hook}"
fi
@k8scat
Copy link
Author

k8scat commented Apr 9, 2021

For example:

bash generate_cert.sh example.com certmaster@example.com "service nginx stop" "service nginx start"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment