由于不少業(yè)務(wù)對(duì)于定時(shí)任務(wù)存在需求,這里打算舉個(gè)
有幸看到stackflow上的一篇文章how-to-run-a-cron-job-inside-a-docker-container故作此文以記錄.
范例:
實(shí)現(xiàn)在Docker Container中設(shè)置cron任務(wù)
范例
在Linux中定時(shí)任務(wù)一般使用cron,ubuntu的官方鏡像中已經(jīng)包含了cron,可以直接使用。

準(zhǔn)備文件:
[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
執(zhí)行構(gòu)建 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
運(yùn)行容器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進(jìn)入docker exec -it 926 /bin/bash
[root@docking ~]# docker exec -it 926 /bin/bash
root@926f9a6ac4b0:/#
查看定時(shí)任務(wù)是否存在和運(yùn)行
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:/#
大功告成~~不過這只是一個(gè)小sample 有興趣的話可以深入研究下,不少linux原生支持crontab的,只不說命令不同罷了。另外,記得要換源~