一、概述
系统管理员,我们经常遇到调度任务的需要。我们可以通过Linux系统中的cron服务来实现这一点。此外,我们可以在容器系统中cron
现在,本教程将泄露在 Docker 容器cron中,在第一种方法中,我们将cron服务嵌入到 docker 镜像中,而另一种方法将说明如何在容器中安装调度服务。
2. Cron 服务——使用Dockerfile方法
Dockerfile构建的是一个创建的镜像容器最简单的时间,我们能做的究竟Dockerfile是什么?大致,是一个简单的文本文件,包含了一系列的构成图的描述。我们需要提供调度任务、cron详细信息,并并列从Dockerfile cron服务。
2.1.写信Dockerfile
让我们快速看一个例子:
$ tree . ├── Dockerfile └── get_date.sh 0 directories, 2 files
Dockerfile的第一行FROM命令开始,该命令可以配置的注册表中获取请求然后的映像。在我们的例子中,默认的配置配置为DockerHub。是MAINTAINER,它是用于生产作者信息的元数据。说明ADD将get_date.sh脚本从主机的镜像构建路径复制到镜像的目标路径。
将脚本复制到RUN构图后,才能获得权限。 细节-操作RUN维护将任何外壳程序当前层的新图像层执行并提交结果。RUN更新apt存储库并在图像中cron作为目标。在crontab cron调度中。
最后,我们将使用CMD指令cron
$ cat Dockerfile
# Dockerfile to create image with cron services
FROM ubuntu:latest
MAINTAINER baeldung.com
# Add the script to the Docker Image
ADD get_date.sh /root/get_date.sh
# Give execution rights on the cron scripts
RUN chmod 0644 /root/get_date.sh
#Install Cron
RUN apt-get update
RUN apt-get -y install cron
# Add the cron job
RUN crontab -l | { cat; echo "* * * * * bash /root/get_date.sh"; } | crontab -
# Run the command on container startup
CMD cron2.2.构建和运行Cron镜像
一旦Dockerfile准备就绪,就我们可以使用docker build命令构建它的镜像就是。点指示泊坞引擎从当前路径(。)Dockerfile Dockerfile上给出的每条指令创建搬运工层,形成以名单最终的构建映像解词的构建输出如下所示:
$ docker build .
Sending build context to Docker daemon 3.072kB
Step 1/8 : FROM ubuntu:latest
---> ba6acccedd29
Step 2/8 : MAINTAINER baeldung.com
---> Using cache
---> e6b3946b2382
Step 3/8 : ADD get_date.sh /root/get_date.sh
---> 4976f058d428
Step 4/8 : RUN chmod 0644 /root/get_date.sh
---> Running in 423a4e9adbab
Removing intermediate container 423a4e9adbab
---> 76d972a082ba
Step 5/8 : RUN apt-get update
---> Running in badc0d84f6ff
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
...
... output truncated ...
...
Removing intermediate container badc0d84f6ff
---> edb8a19b891c
Step 6/8 : RUN apt-get -y install cron
---> Running in efd9b8a67d98
Reading package lists...
Building dependency tree...
...
... output truncated ...
...
Done.
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Removing intermediate container efd9b8a67d98
---> 2b80000d32a1
Step 7/8 : RUN crontab -l | { cat; echo "* * * * * bash /root/get_date.sh"; } | crontab -
---> Running in 1bdd3e0cc877
no crontab for root
Removing intermediate container 1bdd3e0cc877
---> aa7c82aa7c11
Step 8/8 : CMD cron
---> Running in cf2d44873b36
Removing intermediate container cf2d44873b36
---> 8cee091ca87d
Successfully built 8cee091ca87d我们由于已将cron服务预安装到映像中并将任务嵌入到crontab,当因此我们运行容器时cron或者,可以我们使用docker run命令启动容器。随后, “docker run -it选项有助于通过的bash提示符进入容器。
下图显示了cron在容器中的get_date.sh脚本:
$ docker run -it 8cee091ca87d /bin/bash [email protected]:/# [email protected]:/# date Mon Nov 15 14:30:21 UTC 2021 [email protected]:/# ls -ltrh ~/date.out ls: cannot access '/root/date.out': No such file or directory [email protected]:/# ls -ltrh /root/get_date.sh -rw-r--r-- 1 root root 18 Nov 15 14:20 /root/get_date.sh [email protected]:/# crontab -l * * * * * bash /root/get_date.sh [email protected]:/# ls -ltrh ~/date.out -rw-r--r-- 1 root root 29 Nov 15 14:31 /root/date.out [email protected]:/# cat /root/date.out Mon Nov 15 14:31:01 UTC 2021
3. Cron服务——实时容器方法
或者,在 Docker 容器中cron cron。那么,我们的工作方案中可以是什么作业?
让我们使用docker run命令快速运行一个ubuntu通常,容器是轻量级的操作系统,不会cron作为其默认包提供服务。
因此,我们需要**进入容器的外壳,并使用apt存储库命令cron**服务:
$ docker run -it ubuntu:latest /bin/bash Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu 7b1a6ab2e44d: Pull complete Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322 Status: Downloaded newer image for ubuntu:latest root:/# root:/# which cron root:/# apt update -y Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB] ... ... output truncated ... ... All packages are up to date. root:/# apt upgrade -y ... ... output truncated ... ... root:/# apt install cron vim -y Reading package lists... Done ... ... output truncated ... ... Done. invoke-rc.d: could not determine current runlevel invoke-rc.d: policy-rc.d denied execution of start. [email protected]:/# which cron /usr/sbin/cron $ docker cp get_data.sh 77483fc20fc9: /root/get_date.sh
我们可以使用get_date.sh docker cp命令将get_date.sh从主机编辑复制到容器中。下面的配置crontab -e使用vi器每分钟运行一次脚本。另外,输出显示脚本执行的时间脚本:croncron
root:/# export EDITOR=vi [email protected]:/# crontab -e * * * * * bash /root/get_date.sh root:/# date Mon Nov 17 11:15:21 UTC 2021 root:/# ls -ltrh ~/date.out ls: cannot access '/root/date.out': No such file or directory root:/# ls -ltrh /root/get_date.sh -rw-r--r-- 1 root root 18 Nov 17 11:09 /root/get_date.sh root:/# ls -ltrh ~/date.out -rw-r--r-- 1 root root 29 Nov 17 11:16 /root/date.out root:/# cat /root/date.out Mon Nov 17 11:16:01 UTC 2021
4 结论
总而言之,我们已经探索了cron作业的具体细节。使用Dockerfile的方法是将cron服务和任务cron调度配置自动执行脚本。
尽管cron可以在正在运行的容器中安装和配置,但它只是一个运行时构造,可我们使用docker commit构造图。
另外,根据我们的使用情况,这两种选择各自的优势。
0 评论