Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Navneet Karnani
Navneet Karnani

Posted on • Originally published atblog.mandraketech.in on

     

systemd, crond and Docker Container

Recently, I was in a situation where I needed cron jobs to run inside my DevContainer, to mimic the behaviour of my production machine. Specifically, in the context of log rotation. And, given that the default container images do not contain thesystemd process on AmazonLinux 2023, a CentOS 8 based distribution ( my environment of choice ), I had to find a way to make it work.

Thankfully, ChatGPT came to my rescue. It gave me a good starting point and allowed me to create a repository for this, in a more polished and "best practices friendly" structure.

First, the objectives. I wanted to have asystemd run environment, that would runcron jobs, and other services, inside my DevContainer.

This required me to create a Dockerfile that would install thesystemd service, andcronie to run cron jobs.

FROM amazonlinux:2023# Install required packagesRUN yum -y update && \    yum -y install systemd cronie tar gzip git && \    systemctl enable crond.service
Enter fullscreen modeExit fullscreen mode

After this, I can now initialize my image to have the cron configurations:

# Configure crondCOPY cron.d/* /etc/cron.d
Enter fullscreen modeExit fullscreen mode

And finally, start thesystemd process as the CMD for the container. This is a blocking process and ensures the container is not killed immediately:

# Start cron and systemdCMD /usr/sbin/init
Enter fullscreen modeExit fullscreen mode

There you go. We are now ready to run the container. Just one more thing.

To enable systemd to do its job, this container needs to run in privileged mode. So, my docker-compose.yml looks like this:

services:  crond-service:    build:      context: docker-context      dockerfile: Dockerfile    privileged: true
Enter fullscreen modeExit fullscreen mode

A sample cron config file, to just print a log message when the system reboots, sits inside thecron.d directory in my docker build context directory.

@reboot root echo "in crond after reboot"
Enter fullscreen modeExit fullscreen mode

To run the container:

docker compose up -d
Enter fullscreen modeExit fullscreen mode

To check that the cron service is running:

docker compose exec crond-service systemctl status crond.service
Enter fullscreen modeExit fullscreen mode

And to connect into the container, while the service is running:

docker compose exec crond-service bash
Enter fullscreen modeExit fullscreen mode

Note that the name of the service in the docker-compose file is:crond-service

Code use in this blog, including the.devcontainer.json and more, is available at:https://github.com/navneetkarnani/docker-crond

This blog originally published athttps://blog.mandraketech.in/systemd-and-docker-container on 25/May/2023 by Navneet Karnani (https://linkedin.com/in/navneetkarnani )

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Freelancer, Tech Veteran (Generalist, Polyglot, Backend/Distributed/Systems). Celebrating 35 yrs of coding, 25 yrs in industry.
  • Location
    Pune, India
  • Education
    Master Computer Science, Pune University, 1997
  • Pronouns
    He/Him
  • Work
    Fractional CTO, Freelance, Consulting ( #opentowork )
  • Joined

More fromNavneet Karnani

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp