如何在独立容器中配置LetsEncrypt-Cerbot

3
我正在寻找有关在docker容器中运行certbot的简单文档,但我只能找到复杂的指南,包括运行certbot+ Web服务器等。官方页面有点无用...https://hub.docker.com/r/certbot/certbot/。我已经将Web服务器与我的网站分开,并且我也想单独运行certbot。
有人能给我一些指导吗?我如何使用/opt/mysite/html的webroot为mysite.com生成证书。
由于我已经使用端口443和80上有服务,所以我考虑使用“主机网络”,如果需要certbot,但我真的不理解为什么它需要访问443,而我的网站已经在443上提供服务了。
我已经找到了类似以下内容的东西来生成certbot容器,但我不知道如何“使用它”或告诉它为我的网站生成证书。
例如:
WD=/opt/certbot
mkdir -p $WD/{mnt,setup,conf,www}
cd $WD/setup
cat << 'EOF' >docker-compose.yaml
version: '3.7'

services:
  certbot:
    image: certbot/certbot
    volumes:
      - type: bind
        source: /opt/certbot/conf
        target: /etc/letsencrypt
      - type: bind
        source: /opt/certbot/www
        target: /var/www/certbot
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
EOF
chmod +x docker-compose.yaml

这个链接内容与我需要的接近(显然我需要将我的域名作为参数传递进去!)。 Letsencrypt + Docker + Nginx:使用Letsencrypt、Docker和Nginx。
 docker run -it --rm \
  -v certs:/etc/letsencrypt \
  -v certs-data:/data/letsencrypt \
  deliverous/certbot \
  certonly \
  --webroot --webroot-path=/data/letsencrypt \
  -d api.mydomain.com

我喜欢保持所有东西都相对独立,因此我希望仅在其自己的容器中运行Certbot,并配置Nginx / Web服务器以单独使用证书,而不需要Certbot自动配置Nginx或在同一堆栈中运行Web服务器。


大致弄明白了,现在只是正确配置的问题(我怀疑是权限的问题?)。如果能在Docker Hub上有一个合适的主页来正确设置这个就太好了! - FreeSoftwareServers
我一直在努力想要让我的答案中的容器保持运行并休眠12小时。目前,我需要每隔大约90天运行容器,但通常是在遇到SSL错误后才发现,而不是主动解决。不过这只是个人的SSL问题,与工作无关,所以我已经有一段时间没有关注了。 - FreeSoftwareServers
1个回答

5

certbot dockerfile给了我一些见解。

基本上,您可以将以下内容附加到您的docker-compose.yaml文件中,就好像在CLI中附加到certbot一样。

请注意"每小时5个身份验证失败的速率限制"并使用staging进行测试

请参阅DockerFileEntrypoint

ENTRYPOINT [ "certbot" ]

Docker-Compose.yaml:

    command: certonly --webroot -w /var/www/html -d www.examplecom -d examplecom --non-interactive --agree-tos -m example@example.com

完整配置示例:
WD=/opt/certbot
mkdir -p $WD/{setup,certbot_logs}
cd $WD/setup
cat << 'EOF' >docker-compose.yaml
version: '3.7'

services:
 certbot:
    container_name: certbot
    hostname: certbot
    image: certbot/certbot
    volumes:
      - type: bind
        source: /opt/certbot/certbot_logs
        target: /var/log/letsencrypt
      - type: bind
        source: /opt/nginx/ssl
        target: /etc/letsencrypt
      - type: bind
        source: ${WEBROOT}
        target: /var/www/html/

    environment:
      - 'TZ=${TZ}'

    command: certonly --webroot -w /var/www/html -d ${DOMAIN} -d www.${DOMAIN} --non-interactive --agree-tos --register-unsafely-without-email ${STAGING}
EOF
chmod +x docker-compose.yaml
cd $WD/setup

变量:
cat << 'EOF'>.env
WEBROOT=/opt/example/example_html
DOMAIN=example.com
STAGING=--staging
TZ=America/Whitehorse
EOF
chmod +x .env

NGinx:
注意:要启动带有SSL的nginx,您需要证书,即使它们是错误的。因此,我将使用旧证书来启动nginx,然后使用certbot获取正确的证书,然后重新启动nginx以加载正确的证书。这仅适用于首次设置。
server {

   listen 80;
   listen [::]:80;
   server_name www.example.com example.com;

 location /.well-known/acme-challenge/ {

   proxy_pass              http://localhost:8575/$request_uri;
   include                 /etc/nginx/conf.d/proxy.conf;

 }

 location / {
   return 301 https://$host$request_uri;
 }

}

server {
   listen 443 ssl;
   listen        [::]:443;
   server_name www.example.com example.com;

#   ssl_certificate /etc/ssl/live/example.com/fullchain.pem;
#   ssl_certificate_key /etc/ssl/live/example.com/privkey.pem;
   ssl_certificate /etc/ssl/fake/fake.crt;
   ssl_certificate_key /etc/ssl/fake/fake.key;

 location / {

   proxy_pass              http://localhost:8575/;
   include                 /etc/nginx/conf.d/proxy.conf;
 }
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接