Docker Container中启动定时任务范例

由于不少业务对于定时任务存在需求,这里打算举个
有幸看到stackflow上的一篇文章how-to-run-a-cron-job-inside-a-docker-container故作此文以记录.
范例:
实现在Docker Container中设置cron任务


范例

在Linux中定时任务一般使用cron,ubuntu的官方镜像中已经包含了cron,可以直接使用。


准备文件:

[root@docking ~]# tree
.
├── crontabfile
├── Dockerfile
└── sources.list

0 directories, 3 files

Dockerfile

FROM ubuntu:latest
MAINTAINER taroballs

COPY sources.list /etc/apt/sources.list
RUN apt-get update && apt-get -y install cron

# Add crontab file in the cron directory
ADD crontabfile /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

RUN crontab /etc/cron.d/hello-cron

# Run the command on container startup
CMD cron && tail -f /var/log/cron.log

crontabfile

* * * * * echo "Hell0 w0r1d for yOu" >> /var/log/cron.log

sources.list

deb http://mirrors.163.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ xenial-backports main restricted universe multiverse

执行构建 docker build --rm -t taroballs/cron-example .

[root@docking ~]# docker build --rm -t taroballs/cron-example .
Sending build context to Docker daemon  46.08kB
Step 1/9 : FROM ubuntu:latest
 ---> 2a4cca5ac898
Step 2/9 : MAINTAINER taroballs
 ---> Running in bbbd26ffce81
Removing intermediate container bbbd26ffce81
 ---> 9f9104b20636
Step 3/9 : COPY sources.list /etc/apt/sources.list
 ---> 547aa2355568
Step 4/9 : RUN apt-get update && apt-get -y install cron
 ---> Running in 59f23f4dce42
Get:1 http://mirrors.163.com/ubuntu xenial InRelease [247 kB]
..............................................
Step 9/9 : CMD cron && tail -f /var/log/cron.log
 ---> Running in dc679cced789
Removing intermediate container dc679cced789
 ---> 05c2f550467c
Successfully built 05c2f550467c
Successfully tagged taroballs/cron-example:latest

运行容器docker run -itd taroballs/cron-example

[root@docking ~]# docker run -itd taroballs/cron-example
926f9a6ac4b035735ed3c3b26755e3335a2f38494e1fdd4af929c1d1c706d908
[root@docking ~]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS               NAMES
926f9a6ac4b0        taroballs/cron-example   "/bin/sh -c 'cron &&…"   21 seconds ago      Up 20 seconds                           boring_ritchie

可以看到容器id为926f9a6ac4b0,故使用exec进入docker exec -it 926 /bin/bash

[root@docking ~]# docker exec -it 926 /bin/bash
root@926f9a6ac4b0:/# 

查看定时任务是否存在和运行

root@926f9a6ac4b0:/# crontab -l   
* * * * * echo "Hell0 w0r1d for yOu" >> /var/log/cron.log
root@926f9a6ac4b0:/# cat /var/log/cron.log 
Hell0 w0r1d for yOu
root@926f9a6ac4b0:/# 

大功告成~~不过这只是一个小sample 有兴趣的话可以深入研究下,不少linux原生支持crontab的,只不说命令不同罢了。另外,记得要换源~

推荐阅读更多精彩内容