好几年前,ssl 免费证书有效期为 3 个月。搞一个能生成证书的环境就比较方便。
构建带 certbot 的 nginx 镜像
- 准备 Dockerfile 文件。
FROM nginx:1.29.3
# 安装 certbot https://certbot.eff.org/instructions?ws=nginx&os=snap
# 也可以在 nginx 之外使用 certbot 镜像 https://hub.docker.com/r/certbot/certbot
RUN apt-get update && \
apt-get install -y python3-venv && \
python3 -m venv /opt/certbot && \
/opt/certbot/bin/pip install certbot certbot-nginx && \
ln -s /opt/certbot/bin/certbot /usr/local/bin/certbot && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
-
打包镜像
docker build -t xr-nginx:1.29.3 . -
运行镜像,生成容器服务。然后打开浏览器,输入 http://localhost 查看情况。
docker run --rm -it -p 80:80 --name xr-nginx xr-nginx:1.29.3
# 查看日志
docker run --rm -it -p 80:80 xr-nginx:1.29.3
# 进入容器
received
然后镜既有 nginx 的功能,还能生成证书。
生成证书
- 生成证书需要域名解析指向服务器。
- 需要挂载好证书存储位置,防止 docker 关闭启动后 丢失证书。( ./conf/nginx/letsencrypt:/etc/letsencrypt)
# 容器外运行
docker exec -i xr-nginx sh -c "certbot -d xxx.com"
# 进入容器运行
docker exec -it xr-nginx bash
certbot -d xxx.com
