简体   繁体   English

使用Nginx的docker certbot容器检索证书时的用户权限问题

[英]User permission problems when retrieving certificates with docker certbot container for nginx

I realised how badly written this question was so I have rewritten the whole ting together with a solution. 我意识到这个问题写得多么糟糕,所以我将整个问题和解决方案一起重写了。

TLDR: I wanted a solution or suggestion on how to get letsencrypt certificates and keys retrieved by the docker certbot/certbot container to be readable by the nginx:latest container. TLDR:我想要一个解决方案或建议,以了解如何让letencrypt docker certbot / certbot容器检索的证书和密钥可以被nginx:latest容器读取。

The reason it is not readable is that the certificates are stored in a folder, typically /etc/letsencrypt/archive/domain/certificates and the folder archive has owner set to root and group set to root with the mode 0700. In addition, the key also has owner set to root and group set to root with mode 0600. 无法读取的原因是证书存储在一个文件夹中,通常是/ etc / letsencrypt / archive / domain / certificates,并且文件夹归档将所有者设置为root,将组设置为root,其模式为0700。密钥还将所有者设置为root,将组设置为root,模式为0600。

The nginx container has pid 0 set to be nginx master process and run by root, but it spawns a worker process which need to read the certificates and keys. Nginx容器将pid 0设置为nginx主进程并由root运行,但是它产生了一个工作进程,需要读取证书和密钥。 This worker process is owned by a unprivileged user. 该工作进程由非特权用户拥有。

DOCKER-COMPOSE config DOCKER-COMPOSE配置

---

version: '3'
services:

  nginx:
    container_name: nginx
    image: nginx:latest
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./data/nginx/conf:/etc/nginx/conf.d
      # The volume under is to provide the DHPARAM file.
      - ./data/nginx/tls:/etc/pki/tls
      - ./data/certbot/conf:/etc/letsencrypt
      - ./data/certbot/www:/var/www/certbot
    # This reloads the certificates every 24h as long as the container is running
    command: "/bin/sh -c 'while :; do sleep 24h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"

#  certbot:
#    container_name: certbot
#    image: certbot/certbot
#    volumes:
#      - ./data/certbot/conf:/etc/letsencrypt
#      - ./data/certbot/www:/var/www/certbot
#    depends_on:
#      - nginx
#    # This checks if the certificates need to be renewed every 12 hours.
#    entrypoint: "/bin/sh -c \"trap exit TERM; while :; do certbot renew; #sleep 12h & wait $${!}; done;\""


NGINX config NGINX配置

server {
  listen 80 default_server;
  server_name _;

  location /.well-known/acme-challenge/ {
    allow all;
    root /var/www/certbot;
  }

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

I have excluded unnecessary lines in the config. 我排除了配置中不必要的行。 After doing the initial retrival of the certificates I will remove the comments in the yaml file so that the certbot container retrieves new certificates automatically the next time I do docker-compose up -d. 在对证书进行初始检索之后,我将删除yaml文件中的注释,以便下次我执行docker-compose up -d时,certbot容器自动检索新证书。

The command I ran after starting the nginx container. 我启动nginx容器后运行的命令。

docker run -it --rm \
  -v /FQPN/certbot/conf:/etc/letsencrypt \
  -v /FQPN/certbot/www:/var/www/certbot \
  certbot/certbot certonly \
  -m EMAILADDRESS \
  --webroot \
  --agree-tos \
  --webroot-path=/var/www/certbot \
  -d DOMAIN

With what you see above, I get valid certificates, but they are only readable by root. 有了上面看到的内容,我得到了有效的证书,但是它们只能由root读取。

I want this setup to retrieve new certificates when needed but if I manually change the ownership and mode on the folders/files which restrict this to root only, then those changes will be undone when new certificates are retrieved. 我希望此设置在需要时检索新证书,但是如果我手动更改文件夹/文件的所有权和模式(将其限制为仅根),则在检索新证书时将撤消那些更改。

I want a solution so that the unprivileged nginx user can read those certificates and keys without without having to do manual work whenever new certificates are retrieved. 我想要一个解决方案,以便无特权的nginx用户可以读取这些证书和密钥,而无需在检索到新证书时进行手动工作。

I checked if there were options in certbot which could be usefull. 我检查了certbot中是否有可能有用的选项。 After doing certbot --help, I saw there exist a certbot -h all option which give you every single option for certbot. 完成certbot --help后,我看到存在一个certbot -h all选项,它为您提供了certbot的每个选项。

In there I found a post-hook option which is only run when new certificates are successfully retrieved. 在这里,我找到了一个挂机后选项,该选项仅在成功检索新证书时才运行。

The solution was to change the following line in the docker-compose yaml file. 解决方案是在docker-compose yaml文件中更改以下行。

entrypoint: "/bin/sh -c \"trap exit TERM; while :; do certbot renew; #sleep 12h & wait $${!}; done;\""

I changed this to the following. 我将其更改为以下内容。

entrypoint: "/bin/sh -c \"trap exit TERM; while :; do certbot renew --post-hook 'chown root:NGINXUID /etc/letsencrypt/live /etc/letsencrypt/archive && chmod 750 /etc/letsencrypt/live /etc/letsencrypt/archive && chown root:NGINXUID /etc/letsencrypt/archive/DOMAIN/privkey*.pem && chmod 640 /etc/letsencrypt/archive/DOMAIN/privkey*.pem'; sleep 12h & wait $${!}; done;\""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 nginx docker容器中cron的问题 - problems with cron in nginx docker container 运行 Docker 容器 [NGINX] 时,来自 Vault CLI 的 .crt 和 .key 证书会导致 TRUSTED CERTIFICATE 错误 - .crt and .key certificates from Vault CLI causes a TRUSTED CERTIFICATE error when running Docker container [NGINX] 为什么要在 docker 容器中运行 certbot? - Why run certbot in a docker container? Nginx、Certbot & Docker 撰写:/etc/nginx/user.conf.d/*.conf:没有这样的文件或目录 - Nginx, Certbot & Docker Compose: /etc/nginx/user.conf.d/*.conf: No such file or directory 当 nginx 尝试从 docker 容器内部读取 SSL 证书时,权限被拒绝 - Permission denied when nginx tries to read SSL certificate from inside docker container Docker 集装箱 SSL 证书 - Docker container SSL certificates 使用用户权限和USB设备启动Docker容器 - Start a Docker container with a user permission and a USB device docker-compose nginx certbot 未找到证书 - docker-compose nginx certbot not found certificate 将ca证书添加到以指定用户身份运行的Tomcat Docker容器 - Adding ca-certificates to a Tomcat Docker container run with a designated user Docker用户访问容器中的文件夹时拒绝权限 - Docker user Permission denied on accessing folder in container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM