diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..9c153f6 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,5 @@ +FROM nginx:latest + +RUN apt-get update \ + && apt-get install -y --no-install-recommends certbot \ + && rm -rf /var/lib/apt/lists/* diff --git a/nginx/README.md b/nginx/README.md new file mode 100644 index 0000000..a0c10d1 --- /dev/null +++ b/nginx/README.md @@ -0,0 +1,62 @@ +# Nginx + Certbot + +基于官方 `nginx:latest`,内置 `certbot`,用于在容器内执行证书申请与续期。 + +## 基础镜像 + +- `nginx:latest` + +## 包含组件 + +| 组件 | 说明 | +|------|------| +| nginx | 官方镜像默认版本 | +| certbot | Let's Encrypt 客户端 | + +## 构建 + +```bash +# 使用构建脚本 +./rebuild.sh nginx + +# 或直接使用 docker build +docker build -t timi/nginx-certbot:latest ./nginx +``` + +## 使用 + +### 运行 Nginx + +```bash +docker run -d --name nginx \ + -p 80:80 \ + -p 443:443 \ + -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro \ + -v $(pwd)/conf.d:/etc/nginx/conf.d:ro \ + -v $(pwd)/certs:/etc/letsencrypt \ + timi/nginx-certbot:latest +``` + +### 申请/续期证书 + +```bash +docker exec -it nginx certbot certonly --nginx -d example.com +``` + +### 申请/续期泛解析证书 + +需要添加 TEXT 解析记录支持 + +```bash +docker exec -it nginx bash +certbot certonly --server https://acme-v02.api.letsencrypt.org/directory --manual --preferred-challenges dns -d *.example.com +``` + +### 查看证书有效期 + +```bash +docker exec -it nginx bash +certbot certificates | awk '/Certificate Name:/ {name=$3} /Expiry Date:/ {printf "%s: %s %s\n", name, $3, $4}' +``` + +证书会写入 `/etc/letsencrypt`,请持久化该目录。