Cron(和systemd)在docker的centos7中

我想在Docker中运行的centos7 OS中运行cron。 当我尝试启动crond时,我得到:

Failed to get D-Bus connection: Operation not permitted 

谷歌search显示,这是因为systemd没有运行。 但是,当我尝试开始,我得到:

 bash-4.2# /usr/lib/systemd/systemd --system --unit=basic.target systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN) Detected architecture x86-64. Set hostname to <7232ef24bdc8>. Initializing machine ID from random generator. Failed to install release agent, ignoring: No such file or directory Failed to create root cgroup hierarchy: Read-only file system Failed to allocate manager object: Read-only file system 

任何人都知道我可以在这里运行crond吗?

你可以用这种方式在docker里面运行cron服务:

在容器里面映射etc / cron.d / crontab文件,crontab文件应该包含你的cronjobs,看下面的cronjob例子:

 @reboot your-commands-here >> /var/log/cron.log 2>&1 @reboot sleep 02 && your-commands-here >> /var/log/cron.log 2>&1 0 * * * * your-commands-here >> /var/log/cron.log 2>&1 

在你的Dockerfile中:

 chmod -R 0664 /etc/cron.d/* # Create the log file to be able to run tail and initiate my crontab file RUN touch /var/log/cron.log && crontab /etc/cron.d/crontab # Run the command on container startup CMD /etc/init.d/cron restart && tail -f /var/log/cron.log # Run the command on container startup CMD /etc/init.d/cron restart && tail -f /var/log/cron.log #start supervisor ENTRYPOINT ["service", "supervisor", "start"] 

在supervisor下运行cron使用这个configuration:

 [program:cron] command= cron -f startsecs = 3 stopwaitsecs = 3 autostart = true autorestart = true 

我做了一个快速检查,是否可以与docker-systemctl-replacement脚本一起工作 。 该脚本所做的是读取* .service文件(无需systemd守护进程),以便知道如何启动和停止服务。

在“yum install -y cronie”之后,我能够“systemctl.py start crond”,在这种情况下,我可以看到正在运行的进程“/ usr / sbin / crond -n”。 可以将systemctl.py安装为默认的CMD,这样它也可以在保存的图像中简单地启动和停止容器。