给debian的docker容器添加crontab定时任务
现在大部分的docke镜像是基于debian
#cat/etc/issue DebianGNU/Linux9\n\l
Docker容器是不支持后台服务的,像systemctlservicecrontab这些后台运行的服务是不能通过
RUNsystemctlstartnginx
类似这种方案实现的,你必须写自己的entrypoint脚本来启动。本文记录了基于Debian的docker容器设置定时任务的方式。
案例背景
我部署一个前端项目,使用nginx镜像,由于官方镜像是基于debian,对比alpine也不觉得体积差多少,于是就使用debian作为容器系统。
Dockerfile看起来是这样的
FROMnginx:1.15.10 MAINTAINERRyanMiao COPYsources.list/etc/apt/sources.list RUNapt-getupdate&&apt-getinstall-ynet-toolsprocpscurlwgetvimtelnetcron、 &&apt-getautoremove&&apt-getclean&&rm-rf/var/lib/apt/lists/* RUNmkdir-p/data/log/nginx&&mkdir-p/data/web&&rm/etc/nginx/conf.d/default.conf ADDdefault.conf/etc/nginx/conf.d/ ADDindex.html/data/web/ ADDclean_log.sh/data/ COPYclean-cron/etc/cron.d/clean-cron RUNchmod755/data/clean_log.sh&&crontab/etc/cron.d/clean-cron ENTRYPOINTnginx&&cron&&/bin/bash
大概就是安装cron,然后替换nginxconfig,然后复制我们的静态文件,最后启动nginx,启动cron。
说说为啥会有定时任务。可以看到我们是有定时清理脚本的,由于nginx自己没提供日志处理模块,所以才有清理脚本。清理脚本需要定时执行,于是就有定时任务,于是发现docker容器是不支持service的。
总的来说一共有以下几步:
install
apt-getinstallcron
addtocrontab
crontab/etc/cron.d/your-crontab
docker启动时启动cron
ENTRYPOINTcron&&xxxxx
ps,很多人还是喜欢alpine做镜像母体的,因为小。但这个Linux命令确实不熟悉的。
总结
以上所述是小编给大家介绍的给debian的docker容器添加crontab定时任务,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!