如何在运行时使用唯一参数调用 Docker 文件中的 运行? (在 Docker 中安装 Let's Encrypt for NGINX。)
How to call RUN in Dockerfile with unique parameters at runtime? (Installing Let's Encrypt for NGINX in Docker.)
我正在尝试创建一个 Docker 文件来设置 Let's Encrypt。我的初级 Docker Compose 项目包含以下三个文件:
- test/docker-compose.yml
- test/nginx/Dockerfile
- test/letsencrypt/Dockerfile
Let's Encrypt 需要做两件事:(1) 在验证域所有权后初始安装网站证书,以及 (2) 使用 cron 作业每 60-90 天定期更新证书。初始安装证书是 Dockerfile 的 运行 命令的理想用例,因为它只在设置时发生一次,而定期更新证书是 Dockerfile 的 ENTRYPOINT 命令的理想用例,因为它需要坚持运行。
但是,如果我在 Docker 文件中包含完整的 "RUN certbot certonly -d example.com ..." 命令,它将无法构建,因为 Let's Encrypt 的 Certbot 应用程序需要 运行ning 网络服务器才能验证域名的有效性。我想解决这个问题的方法是在构建期间调用 "RUN certbot --help",然后在 NGINX 启动后的 运行 时间调用 "RUN certbot certonly -d example.com ..."。
我的 Docker Compose 文件组织如下:
services:
nginx:
...
letsencrypt:
...
environment:
CERTBOT_PARAMETERS: 'certonly -d example.com ...'
depends_on:
- nginx
我的 Let's Encrypt docker 文件是:
FROM ubuntu:bionic
ENV CERTBOT_PARAMETERS="--help"
...
# Install Certs
RUN certbot $CERTBOT_PARAMETERS
...
# Renew Certs
RUN apt-get -y install cron && \
printf "30 */12 * * * root certbot renew > /proc/1/fd/1 2>/proc/1/fd/2\n" > /etc/cron.d/certbot && \
chmod 644 /etc/cron.d/certbot && \
crontab /etc/cron.d/certbot
ENTRYPOINT ["cron", "-f"]
这成功构建,但是当我调用 "docker-compose up -d" 时,所有 运行 都没有安装证书。如果我在 docker 容器内手动调用 "docker exec -it CONTAINER_NAME /bin/bash" 然后 运行 "certbot certonly -d example.com ...",那么证书就会安装。
我假设在 运行 时调用我的 CERTBOT_PARAMETERS 环境变量有问题。也许它不能接受空格?
如何在 运行 时调用与构建时不同的 运行 命令?我应该使用 CMD 而不是 运行 吗?如果是这样,在 Docker 文件的不同阶段调用 CMD 和 ENTRYPOINT 是否可以接受?
Dockerfile 中的 RUN
命令只会在构建映像时执行。
在您的 docker-compose 文件中设置的 CERTBOT_PARAMETERS
环境变量仅在 图像构建后可用。
我不确定你是如何构建图像的,但如果你想传递构建时可用的值,你需要使用构建参数,例如。如果使用 docker-compose 请参阅文档 re ARGS
: https://docs.docker.com/compose/compose-file/#args
which are environment variables accessible only during the build process.
关于 CMD
和 ENTRYPOINT
,您应该仔细阅读它们的用法,因为它们并不相互排斥,请参阅:https://docs.docker.com/engine/reference/builder/#entrypoint
他们控制图像在构建图像后运行的命令,而RUN
仅在期间使用构建。
我正在尝试创建一个 Docker 文件来设置 Let's Encrypt。我的初级 Docker Compose 项目包含以下三个文件:
- test/docker-compose.yml
- test/nginx/Dockerfile
- test/letsencrypt/Dockerfile
Let's Encrypt 需要做两件事:(1) 在验证域所有权后初始安装网站证书,以及 (2) 使用 cron 作业每 60-90 天定期更新证书。初始安装证书是 Dockerfile 的 运行 命令的理想用例,因为它只在设置时发生一次,而定期更新证书是 Dockerfile 的 ENTRYPOINT 命令的理想用例,因为它需要坚持运行。
但是,如果我在 Docker 文件中包含完整的 "RUN certbot certonly -d example.com ..." 命令,它将无法构建,因为 Let's Encrypt 的 Certbot 应用程序需要 运行ning 网络服务器才能验证域名的有效性。我想解决这个问题的方法是在构建期间调用 "RUN certbot --help",然后在 NGINX 启动后的 运行 时间调用 "RUN certbot certonly -d example.com ..."。
我的 Docker Compose 文件组织如下:
services:
nginx:
...
letsencrypt:
...
environment:
CERTBOT_PARAMETERS: 'certonly -d example.com ...'
depends_on:
- nginx
我的 Let's Encrypt docker 文件是:
FROM ubuntu:bionic
ENV CERTBOT_PARAMETERS="--help"
...
# Install Certs
RUN certbot $CERTBOT_PARAMETERS
...
# Renew Certs
RUN apt-get -y install cron && \
printf "30 */12 * * * root certbot renew > /proc/1/fd/1 2>/proc/1/fd/2\n" > /etc/cron.d/certbot && \
chmod 644 /etc/cron.d/certbot && \
crontab /etc/cron.d/certbot
ENTRYPOINT ["cron", "-f"]
这成功构建,但是当我调用 "docker-compose up -d" 时,所有 运行 都没有安装证书。如果我在 docker 容器内手动调用 "docker exec -it CONTAINER_NAME /bin/bash" 然后 运行 "certbot certonly -d example.com ...",那么证书就会安装。
我假设在 运行 时调用我的 CERTBOT_PARAMETERS 环境变量有问题。也许它不能接受空格?
如何在 运行 时调用与构建时不同的 运行 命令?我应该使用 CMD 而不是 运行 吗?如果是这样,在 Docker 文件的不同阶段调用 CMD 和 ENTRYPOINT 是否可以接受?
Dockerfile 中的 RUN
命令只会在构建映像时执行。
在您的 docker-compose 文件中设置的 CERTBOT_PARAMETERS
环境变量仅在 图像构建后可用。
我不确定你是如何构建图像的,但如果你想传递构建时可用的值,你需要使用构建参数,例如。如果使用 docker-compose 请参阅文档 re ARGS
: https://docs.docker.com/compose/compose-file/#args
which are environment variables accessible only during the build process.
关于 CMD
和 ENTRYPOINT
,您应该仔细阅读它们的用法,因为它们并不相互排斥,请参阅:https://docs.docker.com/engine/reference/builder/#entrypoint
他们控制图像在构建图像后运行的命令,而RUN
仅在期间使用构建。