Larry Martell
Larry Martell

Reputation: 3756

Cron (and systemd) in centos7 in docker

I want to run cron in a centos7 OS running in docker. When I try and start crond I get:

Failed to get D-Bus connection: Operation not permitted

Googling shows that that is because systemd is not running. But when I try and start that I get:

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

Anyone know how I can run crond here?

Upvotes: 1

Views: 7447

Answers (2)

Guido U. Draheim
Guido U. Draheim

Reputation: 3271

I did a quick check whether that could work with the docker-systemctl-replacement script. What that script does is to read *.service files (without the help of a systemd daemon) so that it knows how to start and stop a service.

After "yum install -y cronie" I was able to "systemctl.py start crond" in which case I can see a running process "/usr/sbin/crond -n". It is possible to install the systemctl.py as the default CMD so that it will also work on simply starting and stopping the container from a saved image.

Upvotes: 1

user1427258
user1427258

Reputation: 68

You can run cron service inside docker in this manner:

Map etc/cron.d/crontab file inside the container, the crontab file should contains your cronjobs, see cronjob examples below:

@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

In your 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"]

To run cron under supervisor use this config:

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

Upvotes: 0

Related Questions