Cron fails to start in k8s-singleuser-sample

Hi,

Hopefully this is the correct place as I’m experiencing some issues when trying to install cron in Quay.

Here is my existing Dockerfile which is working like a charm and jupyterlab runs successfully

FROM quay.io/jupyterhub/k8s-singleuser-sample:3.2.1

USER root

# requirements for the pip packages
RUN apt-get update \
 && apt-get install --yes --no-install-recommends \
    gcc libpq-dev python3-dev \
 && rm -rf /var/lib/apt/lists/*

COPY requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt

USER ${NB_USER}

ENTRYPOINT ["tini", "--"]
CMD ["jupyter", "lab"]

but when I try to add cron , for example

FROM quay.io/jupyterhub/k8s-singleuser-sample:3.2.1

USER root

# requirements for the pip packages
RUN apt-get update \
 && apt-get install --yes --no-install-recommends \
    gcc libpq-dev python3-dev cron\
 && rm -rf /var/lib/apt/lists/*

# Copy crontab file to the cron.d directory
COPY crontab-example /etc/cron.d/crontab
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/crontab
# Apply cron job
RUN crontab /etc/cron.d/crontab
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
RUN service cron start
#RUN sudo journalctl -u cron.service
RUN service cron status

COPY requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt

USER ${NB_USER}

ENTRYPOINT ["tini", "--"]
CMD ["jupyter", "lab"]

building fails with

 => ERROR [7/9] RUN service cron status                                                                       0.1s
------
 > [7/9] RUN service cron status:
0.106 cron is not running ... failed!
ERROR: failed to solve: process "/bin/sh -c service cron status" did not complete successfully: exit code: 1

UPDATE: When trying to start service from docker and not start it via dockerfile, i get

Starting periodic command scheduler: croncron: can't open or create /var/run/crond.pid: Permission denied
 failed!

Any ideas or is there a better way to do it?

Thanks in advance for the support and help
paparoup

Most container images don’t support the usual operating system services. They’re intended to be minimal systems that just provide the required applications.

It’s possible to get cron to run in a container, see these previous topics:

Hi and thanks for replying,

Yeap , I totally get your point with minimal images but there are cases that at least cron is needed but I will also try to find another way.

In the meantime , If you check the Dockerfile example, have followed the exact link that you shared with the change being only replacing how cron starts

CMD cron

with

RUN service cron start

in the root part in order to check if the service would start but doesn’t seem to and cron service fails to start.

Having two CMD parts, one for root and one for user wouldn’t be a problem right ? Dockerfile wise i mean.

Typically (though not always, it depends on the operating system) this requires a service manager to be running.

No, you can only have a single CMD. You’ll need to find a way to run both cron and jupyterhub-singleuser from that command.

What cron jobs do you need to run? There may be easier ways to do it.

Hi and thanks for getting back

I fixed the CMD issue by doing this

CMD ["tini", "-s", "--", "/bin/sh", "-c", "/etc/init.d/cron start & jupyter lab"]

and tried some workarounds so that jovyan user can have a fixed cronjob /var/spool/cron/crontabs/jovyan where users can add their own entries

COPY crontab-example /var/spool/cron/crontabs/jovyan
RUN chmod 0600 /var/spool/cron/crontabs/jovyan && \
    chown jovyan:crontab /var/spool/cron/crontabs/jovyan
    chmod u+s /usr/sbin/cron

but even though it works locally , when applied in k8s the cron service isn’t started

Documented the above for anyone that may end up in this thread due to troubleshooting

That said,I don’t think that my initial thinking makes any sense as I was trying to create a VM-like crontabs in images that are minimal for a reason and should stay like this. So I have installed Jupyter Scheduler — jupyter_scheduler documentation and now users can do whatever they want via python notebooks/scripts.

Thanks a lot for taking the time, I would say that my initial plan is rejected :smiley:

1 Like