cron job won’t run into my Docker container launched by Visual Studio

  Kiến thức lập trình

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”, but I think it’s not needed anymore given that I tried to register the job to the root user with RUN crontab -u app /etc/cron.d/mycronjob

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 🙂

thank you
Sergio

I tried almost everything find on google, I’d like to see my useless message added to the file

New contributor

Sergio Tecflora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT