如何在certbot更新后从docker内部重启主机nginx

问题描述 投票:0回答:1

如果您有一个运行certbot的docker容器,但是一个nginx实例使用了主机上运行的那些证书,那么如何从docker容器内部重新启动主机nginx?

这是正在运行的容器

certbot:
    image: certbot/dns-ovh
    container_name: certbot
    volumes:
      - /etc/letsencrypt/:/etc/letsencrypt
      - /var/lib/letsencrypt:/var/lib/letsencrypt
      - /root/.secrets/certbot-ovh.ini:/root/.secrets/ovh-creds.ini
    entrypoint: /bin/sh -c 'trap exit  TERM; while :; do certbot renew sleep 12h & wait $${!}; done;'
docker nginx docker-compose lets-encrypt certbot
1个回答
0
投票

您必须在renew命令中添加--post-hook,该命令使用ssh将nginx reload命令发送到主机。

为此,容器需要使用network_mode: "host"运行

然后,在启动/重新创建容器时,需要同时包含sshpassopenssh。这是用完成的apk add openssh sshpass

然后在挂机中,您需要SSH进入主机并重新加载nginx

sshpass -p 'your password' ssh -o 'StrictHostKeyChecking no' root@localhost 'systemctl reload nginx'

假设您具有root用户访问权限。这使用sshpass在ssh中输入密码,从而跳过“您是否要添加指纹”消息,并将relaod命令发送到localhost

全部放入docker-compose文件中,如下所示:

  certbot:
    image: certbot/dns-ovh
    container_name: certbot
    network_mode: "host"
    volumes:
      - /etc/letsencrypt/:/etc/letsencrypt
      - /var/lib/letsencrypt:/var/lib/letsencrypt
      - /root/.secrets/certbot-ovh.ini:/root/.secrets/ovh-creds.ini
    entrypoint: >
      /bin/sh -c 
      'apk add openssh sshpass &&
      trap exit  TERM; while :;
      do certbot renew --post-hook 
      "sshpass -p '"'"'your password'"'"' ssh -o '"'"'StrictHostKeyChecking no'"'"' root@localhost '"'"'systemctl reload nginx'"'"'";
      sleep 12h & wait $${!}; done;'

>此处允许根据需要编写任意数量的缩进线,而无需添加转义的notehr层。稍后还将这些行合并为一行。

此处使用的'"'"'用于转义--post-hook内的单字',它关闭第一个单引号,打开一个新的双引号,其中包含一个单引号,然后打开单引号再次。

© www.soinside.com 2019 - 2024. All rights reserved.