AI获取知识变容易了,不代表理解知识变深了。

核心内容摘要

RabbitMQ如何保证消息可靠性
ClawdBot新手教程:如何安全修改clawdbot.json中的API密钥与端口

OFA-VE开箱即用:无需配置的赛博风格视觉分析工具

当您想要安排计划任务可以使用内置在 macOS 和 Linux 中的常见工具比如 cron或者像 AWS Lambda 这样的特殊工具。

Cron 不如 AWS Lambda 强大但它在 Unix 系统的后台任务中工作得很好特别是在使用容器的情况下。

然而对于 Docker 来说这有点复杂因为不能简单地从终端开始新的 cron 作业并期望它工作。

How to Dockerize a Cron Job要在 Docker 容器中运行 cron 作业您需要使用 cron 并在 Docker 容器的前台运行它。

下面是一个如何设置的例子Create Cron File创建一个文件其中包含要在 Docker 容器下运行的所有 cron 作业。

cat cron我们的示例文件如下:* * * * * echo Current date is date /var/log/cronCreate Dockerfile接下来创建一个安装 cron 服务的Dockerfile并将脚本复制到容器。

在这里我们提供了 3 个 Dockerfile 示例它们使用不同的操作系统。

Dockerfile with Alpine LinuxFROM alpine:3 # Copy cron file to the container COPY cron /etc/cron.d/cron # Give the permission RUN chmod 0644 /etc/cron.d/cron # Add the cron job RUN crontab /etc/cron.d/cron # Link cron log file to stdout RUN ln -s /dev/stdout /var/log/cron # Run the cron service in the foreground CMD [ crond, -l, 2, -f ]Dockerfile with Apache and PHPFROM php:

0-apache # Install cron RUN apt update \ apt -y install cron # Copy cron file to the container COPY cron /etc/cron.d/cron # Give the permission RUN chmod 0644 /etc/cron.d/cron # Add the cron job RUN crontab /etc/cron.d/cron # Link cron log file to stdout RUN ln -s /dev/stdout /var/log/cron # Start cron service RUN sed -i s/^exec /service cron start\n\nexec / /usr/local/bin/apache2-foregroundDockerfile with Ubuntu LinuxFROM ubuntu:latest # Install cron deamon RUN apt update apt install -y cron # Copy cron file to the container COPY cron /etc/cron.d/cron # Give the permission RUN chmod 0644 /etc/cron.d/cron # Add the cron job RUN crontab /etc/cron.d/cron # Link cron log file to stdout RUN ln -s /dev/stdout /var/log/cron # Run the cron service in the foreground CMD [cron, -f]Build and Run Container当前目录中有两个文件一个是 cron 它包含了 cronjob。

一个是 Dockerfile 它有 Docker 的构建指令。

运行以下命令使用 Dockerfile 构建 Docker 镜像。

docker build -t my_cron .镜像构建成功后启动容器docker run -d my_cron这将启动容器下的 cron 守护进程它将执行 cron 文件中定义的所有计划作业。

Test Setup我们已经链接了 cron 日志文件/var/log/cron到/dev/stdoutCron 服务生成的所有日志可以使用docker logs命令查看。

首先使用docker ps命令查找容器 id 或名称。

docker ps然后检查 Docker 容器的日志文件。

docker logs container_id在 cronjobs 中我打印了当前日期并把它们写入日志中。

输出如上所示这意味着 cron 作业在 Docker 容器下正常运行。

我的开源项目course-tencent-cloud酷瓜云课堂 - gitee仓库course-tencent-cloud酷瓜云课堂 - github仓库

三妻四妾泰剧荷尔蒙免费观看-三妻四妾泰剧荷尔蒙免费观看应用

百度百家号客服电话人工服务

123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123