温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么给debian的docker容器添加crontab定时任务

发布时间:2022-10-11 14:47:03 来源:亿速云 阅读:738 作者:iii 栏目:服务器

本篇内容主要讲解“怎么给debian的docker容器添加crontab定时任务”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么给debian的docker容器添加crontab定时任务”吧!

现在大部分的docke镜像是基于debian

# cat /etc/issue
debian gnu/linux 9 \n \l

docker容器是不支持后台服务的,像systemctl service crontab这些后台运行的服务是不能通过

run systemctl start nginx

类似这种方案实现的,你必须写自己的entrypoint脚本来启动。本文记录了基于debian的docker容器设置定时任务的方式。

案例背景

我部署一个前端项目,使用nginx镜像,由于官方镜像是基于debian,对比alpine也不觉得体积差多少,于是就使用debian作为容器系统。

dockerfile看起来是这样的

from nginx:1.15.10
maintainer ryan miao

copy sources.list  /etc/apt/sources.list
run apt-get update && apt-get install -y net-tools procps curl wget vim telnet cron 、
  && apt-get autoremove && apt-get clean && rm -rf /var/lib/apt/lists/*

run mkdir -p /data/log/nginx && mkdir -p /data/web && rm /etc/nginx/conf.d/default.conf
add default.conf /etc/nginx/conf.d/
add index.html /data/web/

add clean_log.sh /data/
copy clean-cron /etc/cron.d/clean-cron
run chmod 755 /data/clean_log.sh && crontab /etc/cron.d/clean-cron

entrypoint nginx && cron && /bin/bash

大概就是安装cron,然后替换nginx config, 然后复制我们的静态文件,最后启动nginx, 启动cron。

说说为啥会有定时任务。可以看到我们是有定时清理脚本的,由于nginx自己没提供日志处理模块,所以才有清理脚本。清理脚本需要定时执行,于是就有定时任务,于是发现docker容器是不支持service的。

总的来说一共有以下几步:

install

apt-get install cron

add to crontab

crontab /etc/cron.d/your-crontab

docker启动时启动cron

entrypoint cron && xxxxx

ps, 很多人还是喜欢alpine做镜像母体的,因为小。但这个linux命令确实不熟悉的。

到此,相信大家对“怎么给debian的docker容器添加crontab定时任务”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI