skip to Main Content

since a couple of days I’m trying to create a simple docker that uses cron to execute a scheduled job. I’ve read almost every post found on google but I’m still stucked.

This is my Dockerfile:

FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
RUN apt-get update
RUN apt-get clean # reduce image size
RUN apt-get install -yqq cron

COPY ["DockerWorkerServiceScheduledExecutable2/cron.allow", "/etc/"]
COPY ["DockerWorkerServiceScheduledExecutable2/mycronjob", "/etc/cron.d/"]

RUN sed -i 's/r$//' /etc/cron.allow
RUN chown -R root:crontab /etc/cron.allow
RUN chmod 0644 /etc/cron.allow
RUN sed -i 's/r$//' /etc/cron.d/mycronjob
RUN chmod 0644 /etc/cron.d/mycronjob

RUN crontab /etc/cron.d/mycronjob
RUN touch /var/log/cron.log
CMD /etc/init.d/crond start
CMD /etc/init.d/cron start && tail -f /var/log/cron.log
CMD /usr/sbin/cron -f
CMD cron

USER app
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["DockerWorkerServiceScheduledExecutable2/DockerWorkerServiceScheduledExecutable2.csproj", "DockerWorkerServiceScheduledExecutable2/"]
RUN dotnet restore "./DockerWorkerServiceScheduledExecutable2/./DockerWorkerServiceScheduledExecutable2.csproj"
COPY . .
WORKDIR "/src/DockerWorkerServiceScheduledExecutable2"
RUN dotnet build "./DockerWorkerServiceScheduledExecutable2.csproj" -c $BUILD_CONFIGURATION -o /app/build

CMD /etc/init.d/cron start && tail -f /var/log/cron.log
CMD /usr/sbin/cron -f

This is my cron.allow, that I used trying to register the job for the user "app" with RUN crontab -u app /etc/cron.d/mycronjob, but I think it’s not needed anymore given that I tried to register the job to the root user

root
app

And this is the file "mycronjob"

*/1 * * * * dotnet /app/bin/Debug/net8.0/DockerWorkerServiceScheduledExecutable2.dll >> /var/log/cron.log 2>&1

Last one, this is the Program.cs of the DockerWorkerServiceScheduledExecutable2 project

if (!File.Exists("myfile.txt"))
{
    File.Create("myfile.txt").Close();
}
await File.AppendAllTextAsync("myfile.txt", "DoSomethingUseless: " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + System.Environment.NewLine + "rn");
Console.WriteLine("DoSomethingUseless: " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));

I’m a newbie in the linux and docker world so I made a mess probably(99% sure).
The main effect is that if I check cron service status with service cron status I got "cron is not running … failed!"

I know that (probably) this commands do the same stuff, but I was not sure which one was the right one, which was the right row, and, at the end, none of them seems to works 🙁

CMD /etc/init.d/crond start
CMD /etc/init.d/cron start && tail -f /var/log/cron.log
CMD /usr/sbin/cron -f
CMD cron

Can anyone help me to fix that? Any hint would be appreciated too 🙂
As I said initially tried almost everything found on google, I’d like to see my useless message added to the file

thank you

Sergio

2

Answers


  1. Chosen as BEST ANSWER

    I have made a step forward, the problem seems to be the "starting image", sorry if I use the wrong name but I don't know how to call it:

    FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
    

    this is my new Dockerfile that almost does what I want:

    FROM ubuntu:latest
    MAINTAINER [email protected]
    
    RUN apt-get update && apt-get -y install cron && apt-get -y install dotnet8
    
    # Copy hello-cron file to the cron.d directory
    # COPY hello-cron /etc/cron.d/hello-cron
    COPY ["DockerWorkerServiceScheduledExecutable3/mycronjob", "/etc/cron.d/mycronjob"]
    RUN sed -i 's/r$//' /etc/cron.d/mycronjob
    # Give execution rights on the cron job
    RUN chmod 0644 /etc/cron.d/mycronjob
    
    # Apply cron job
    RUN crontab /etc/cron.d/mycronjob
     
    # Create the log file to be able to run tail
    RUN touch /var/log/cron.log
     
    # Run the command on container startup
    # CMD cron && tail -f /var/log/cron.log
    CMD service cron start && tail -f /var/log/cron.log
    
    WORKDIR /app
    
    # FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
    ARG BUILD_CONFIGURATION=Release
    WORKDIR /src
    COPY ["DockerWorkerServiceScheduledExecutable3/DockerWorkerServiceScheduledExecutable3.csproj", "DockerWorkerServiceScheduledExecutable3/"]
    RUN dotnet restore "./DockerWorkerServiceScheduledExecutable3/./DockerWorkerServiceScheduledExecutable3.csproj"
    COPY . .
    WORKDIR "/src/DockerWorkerServiceScheduledExecutable3"
    RUN dotnet build "./DockerWorkerServiceScheduledExecutable3.csproj" -c $BUILD_CONFIGURATION -o /app/build
    

    Now the problem is that cron doesn't start automatically, I have to run

    service cron start
    

    to make it working


  2. another step 😀
    to make the cron starts at startup I had to create the file "docker-compose.vs.debug.yml" in the same directory of "docker-compose.yml" and add this:

    version: '3.4'
    
    services:
      dockerworkerservicescheduledexecutable3:
        image: ${DOCKER_REGISTRY-}dockerworkerservicescheduledexecutable3
        build:
          context: .
          dockerfile: DockerWorkerServiceScheduledExecutable3/Dockerfile
        entrypoint: sh -c '/etc/init.d/cron start && tail -f /var/log/cron.log'
    

    in this way in Debug mode it works as hoped.
    Now I have to face the Release mode

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search