使用 alpine/openssl
镜像生成证书
1. 拉取容器
shell
[root@localhost ~]# docker run --rm alpine/openssl version
OpenSSL 3.3.3 11 Feb 2025 (Library: OpenSSL 3.3.3 11 Feb 2025)
2. 运行 alpine/openssl
生成证书(Nginx)
shell
# 生成1个.key私钥文件,1个.crt的证书文件,存放在当前路径
[root@localhost ~]# docker run --rm -v $(pwd):/certs alpine/openssl req -x509 -newkey rsa:4096 -keyout /certs/server.key -out /certs/server.crt -days 365 -nodes -subj "/CN=vw.land.com"
解释命令:
docker run --rm: 运行一个容器并在完成后自动删除它。
-v $(pwd):/certs: 将当前目录挂载到容器内的 /certs 目录。
alpine/openssl: 使用的镜像。
req -x509: 生成自签名证书。
-newkey rsa:4096: 生成一个新的 RSA 4096 位的私钥。
-keyout /certs/key.pem: 指定私钥的输出路径。
-out /certs/cert.pem: 指定证书的输出路径。
-days 365: 设置证书的有效期为 365 天。
-nodes: 不加密私钥。
-subj "/CN=vw.land.com": 设置证书的主题(Common Name 为 vw.land.com)。
3. 查看证书
shell
[root@localhost ~]# ll
-rw-r--r--. 1 root root 1814 3月 6 16:27 cert.pem
-rw-------. 1 root root 3272 3月 6 16:27 key.pem