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
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:
this is my new Dockerfile that almost does what I want:
Now the problem is that cron doesn't start automatically, I have to run
to make it working
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:
in this way in Debug mode it works as hoped.
Now I have to face the Release mode