How to Fix “no crontab for root” Error in Docker Containers with Node.js Cron Jobs


4 views

When setting up cron jobs in a Docker container based on the older Node.js 0.10 image, you might encounter the frustrating "no crontab for root" error. This typically occurs when trying to initialize cron jobs without proper initialization of the crontab system.

The original Dockerfile has several anti-patterns that contribute to this issue:


# Problematic original CMD instructions
CMD cron -f
CMD touch /var/log/cron.log && sh /pulse/crontab.sh && tail -f /var/log/cron.log
CMD cron /pulse/cron.jobs
CMD crontab -l

Each CMD instruction overwrites the previous one, leaving only the last one (crontab -l) to execute. This explains why the cron jobs aren't being properly initialized.

Here's a corrected approach that works reliably:


FROM node:0.10

RUN apt-get update && apt-get install -y cron

WORKDIR /pulse
COPY . .

# Set up cron environment
RUN touch /var/log/cron.log
RUN (crontab -l 2>/dev/null; echo "") | crontab -

# Make script executable
RUN chmod +x /pulse/crontab.sh

# Single CMD that handles initialization
CMD /pulse/crontab.sh && cron && tail -f /var/log/cron.log

The cron initialization script needs to handle the case where no crontab exists:


#!/bin/bash

# Create cron jobs file
cat > /pulse/cron.jobs < /tmp/awsPulseTest.log 2>&1
EOF

# Initialize crontab if empty
(crontab -l 2>/dev/null || echo "") | cat - /pulse/cron.jobs | crontab -

When working with cron in Docker containers:

  • Ensure cron has write permissions to its working directory
  • Use absolute paths for all cron job commands
  • Redirect output to log files for debugging
  • Consider using supervisord for managing multiple processes
  • Test cron jobs manually before containerizing them

If issues persist:


# Check cron status
service cron status

# View current crontab
crontab -l

# Check cron logs
tail -f /var/log/syslog

When working with Docker containers that need to run scheduled tasks, you might encounter the frustrating no crontab for root error. This typically happens when trying to set up cron jobs inside a Docker container, especially when using the node base image.

The error occurs because:

  • The cron service isn't properly initialized in the container
  • The crontab file doesn't exist for the root user
  • Multiple CMD instructions in Dockerfile cause only the last one to execute

Here's a corrected version of your Dockerfile:

FROM node:0.10
MAINTAINER Tom

VOLUME /var/log/

RUN mkdir /pulse
ADD . /pulse
WORKDIR /pulse

# Install cron and ensure it runs in foreground
RUN apt-get update && apt-get install -y cron

# Set up cron jobs
ADD *.sh /pulse/
RUN chmod 750 /pulse/crontab.sh

# Create log file and set up cron
RUN touch /var/log/cron.log

# Single CMD that combines all operations
CMD sh /pulse/crontab.sh && cron -f && tail -f /var/log/cron.log

Your script can be enhanced to handle the case when no crontab exists:

#!/bin/bash

# Create cron jobs file
cat <<- 'EOF' > cron.jobs
0 * * * * node /pulse/scripts/awsPulseTest.js > /tmp/awsPulseTest.log 2>&1
EOF

# Ensure crontab exists
crontab -l >/dev/null 2>&1 || echo "" | crontab -

# Load the jobs
crontab cron.jobs
  • Removed multiple CMD instructions (Docker only executes the last one)
  • Added proper initialization of crontab if it doesn't exist
  • Combined operations in a single CMD to ensure proper execution order
  • Kept cron running in foreground with -f flag

After rebuilding your container, you can verify it's working by:

docker exec -it your_container_name bash -c "crontab -l"

This should display your configured cron jobs without the "no crontab" error.

For more robust cron job management in containers, consider using supercronic:

FROM node:0.10
RUN apt-get update && apt-get install -y wget
RUN wget https://github.com/aptible/supercronic/releases/download/v0.1.12/supercronic-linux-amd64 \
    -O /usr/local/bin/supercronic && \
    chmod +x /usr/local/bin/supercronic

ADD crontab /etc/crontab
CMD ["supercronic", "/etc/crontab"]