几天以来,我一直在尝试创建一个简单的 docker,它使用 cron 来执行计划的作业。我几乎阅读了谷歌上找到的所有帖子,但我仍然陷入困境。
这是我的 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
这是我的 cron.allow,我曾经尝试使用
RUN crontab -u app /etc/cron.d/mycronjob
为用户“app”注册作业,但考虑到我尝试将作业注册到 root 用户,我认为不再需要它了
root
app
这是文件“mycronjob”
*/1 * * * * dotnet /app/bin/Debug/net8.0/DockerWorkerServiceScheduledExecutable2.dll >> /var/log/cron.log 2>&1
最后一张,这是DockerWorkerServiceScheduledExecutable2项目的Program.cs
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 + "\r\n");
Console.WriteLine("DoSomethingUseless: " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
我是 Linux 和 docker 世界的新手,所以我可能搞砸了(99%确定)。 主要效果是,如果我使用
service cron status
检查 cron 服务状态,我会得到“cron 未运行...失败!”
我知道(可能)这个命令做同样的事情,但我不确定哪一个是正确的,哪一行是正确的,最后,它们似乎都不起作用:(
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
有人可以帮我解决这个问题吗?任何提示也将不胜感激:) 正如我所说,最初尝试了在谷歌上找到的几乎所有内容,我希望看到我的无用消息添加到文件中
谢谢你
塞尔吉奥
我已经向前迈出了一步,问题似乎出在“启动机器”上,抱歉,如果我用错了名字,但我不知道如何称呼它:
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
这是我的新 Dockerfile,它几乎可以满足我的需求:
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
现在的问题是 cron 没有自动启动,我必须运行
service cron start
使其发挥作用