Alpine cronjob error setpgid: Operation not permitted

I am completely new to Docker and Linux and I am trying to make a simple Alpine based image that runs a cronjob every minute. That is, it should pint “Hello” every minute.

Steps to reproduce:

Make a docker file with the following content:

# Use an official Alpine image as a parent image
FROM alpine:latest

# Install cron and any other dependencies you might need
RUN apk update && \
    apk add dcron wget curl logrotate

# Add crontab file in the cron directory
ADD crontab /etc/crontabs/root

# Give execution rights on the cron job
RUN chmod 0644 /etc/crontabs/root

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup using the JSON array syntax
CMD ["sh", "-c", "env > /var/log/cron.env && crond -f -d 8"]

Make a file called crontab and save it in the same folder as the dockerfile:

* * * * * /bin/echo "Hello" >> /var/log/cron.log 2>&1

Build the image with the command:

docker build -t my-alpine-cron .

Run the image and you should see the error message:

2024-07-28 13:10:44 setpgid: Operation not permitted

Why am I getting this error? What can I do to solve it?

OS Version/build: Windows 10 x64
App version: Docker Desktop 4.32.0

cron requires capabilities, that either need to be added using cap-add, or by running the container in privileged mode (absolutely not recommended!).

Personally, I prefer this container friendly alternative:

It can be executed with an arbitrary user id and is crontab-compatible.

1 Like