0

I am using a Debian 11 container in which I have installed cron tool. I am using a custom Dockerfile:

FROM debian:11-slim

RUN apt-get update && \
    apt-get upgrade --yes && \
    apt-get install --no-install-recommends cron -y && \
    apt-get install --no-install-recommends python3 -y && \
    apt install --no-install-recommends python3-pip -y && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

RUN service cron start

I am starting the cron service, it seems to be ok but no crontab schedule will be executed. Where can I find the cron logs? I have no /var/log/syslog

rschirin
  • 1,939
  • 10
  • 34
  • 44

1 Answers1

0

The problem with your Dockerfile above is that you put the service cron start with the RUN directive. This directive specify that the command will only run on the Dockerfile build step, not when you are executing the built image. To understand the difference between RUN, CMD, EXEC directives of docker, refer to this link.

A very simple way of collecting log from cron, which makes use of the Docker's log collecting capability is to redirect the output of cron scripts to stdout. Here is a gist that contains the solution. Basically there is two part of the solution

  • Specify the cron process as the entrypoint of the docker image.
  • Make changes to the crontab file to redirect the output of cron jobs to stdout of the main cron process.

This gist is written for the Ubuntu image, so it should work or require little modification on Debian image. However, if your ultimate goal is to run cron in your image, I recommend using alpine. See stackoverflow answer for more information for this approach.

antran22
  • 31
  • 1
  • 5
  • Cron service is running when I start my container. So crontab jobs should be executed. Unfortunately this is not happening so I have to check any cron errors or messages. – rschirin Jul 10 '22 at 19:56
  • 1
    It is true that your crontab will be running when you start your container, but it will be in the background and not because of the `RUN service cron start` directive My answer show you how to bring that `cron` service to be the main entrypoint of the image, and redirect the output of cronjobs to the main stdout so you can view it with Docker's log collecting facility – antran22 Jul 11 '22 at 03:55