【问题标题】:How to restart host nginx from inside docker after certbot renewcertbot更新后如何从docker内部重新启动主机nginx
【发布时间】:2020-04-25 13:23:58
【问题描述】:

如果你有一个运行 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】:

    你必须在renew命令中添加一个--post-hook,它使用ssh发送nginx reload命令到主机。

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

    那么你需要在启动/重新创建容器时安装sshpassopenssh。这是完成的 apk add openssh sshpass

    然后在 post-hook 中你需要 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;'
    

    这里的> 允许编写尽可能多的缩进行,而无需添加转义层。它还会在以后将这些行合并为一行。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-07
      • 2021-06-12
      • 2022-10-02
      • 2021-12-28
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多