From 571e0f32825afd63dd82a5462e0f7f3d70f2d9dc Mon Sep 17 00:00:00 2001 From: Alex Hyett Date: Thu, 6 Jul 2017 10:03:51 +0100 Subject: [PATCH] Initial commit --- Dockerfile | 28 ++++++++++++++++++++++++++++ Program.cs | 12 ++++++++++++ README.md | 21 +++++++++++++++++++++ docker-compose.yml | 6 ++++++ dotnet-cron.csproj | 8 ++++++++ schedule | 11 +++++++++++ 6 files changed, 86 insertions(+) create mode 100644 Dockerfile create mode 100644 Program.cs create mode 100644 README.md create mode 100644 docker-compose.yml create mode 100644 dotnet-cron.csproj create mode 100644 schedule diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fc61780 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +FROM microsoft/dotnet:1.1.1-sdk + +ARG BUILDCONFIG=DEBUG + +# Install Cron +RUN apt-get update -qq && apt-get -y install cron -qq --force-yes + +# Copy project files to avoid restoring packages if they haven't changed +COPY . /app/. +WORKDIR /app/ + +# Set up schedule +ADD schedule /etc/cron.d/schedule +RUN chmod 0644 /etc/cron.d/schedule + +# Create log file +RUN touch /var/log/cron.log +RUN chmod 0666 /var/log/cron.log + +# Publish dotnet core app +RUN dotnet restore +RUN dotnet publish -c $BUILDCONFIG -o out + +# Volume required for tail command +VOLUME /var/log + +# Run Cron +CMD /usr/sbin/cron && tail -f /var/log/cron.log \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..65043a7 --- /dev/null +++ b/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace test_dotnet +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine($"This program should be running on a schedule: {DateTime.Now}"); + } + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..4e26d42 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# .Net Core console app running in a schedule in Docker + +This is an example project showing you how to run a console app in a schedule in a docker container. + +The docker image makes use of cron which is a unix utility for running commands on a schedule. + +The docker file attached does the following: +1. Installs cron on the standard microsoft/dotnet:1.1.1-sdk image. +2. Copies your application files across to the docker image. +3. Copies the schedule file across to the docker image. +4. Restores and Publishes your dotnet core image. +5. Runs cron and outputs the contents of the /var/log/cron.log file to the screen. + +## How to use +To use this within your own dotnet core console app you will need the Dockerfile, schedule file dockerignore file and docker-compose.yml (if you are not using your own). + +You need to modify the schedule file to reference the dll of your console app. + +Then run: + +`docker-compose up` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ccb3864 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +version: "3" + +services: + console: + build: . + container_name: recurring-console diff --git a/dotnet-cron.csproj b/dotnet-cron.csproj new file mode 100644 index 0000000..abb9969 --- /dev/null +++ b/dotnet-cron.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp1.1 + + + diff --git a/schedule b/schedule new file mode 100644 index 0000000..cc2cb6b --- /dev/null +++ b/schedule @@ -0,0 +1,11 @@ +* * * * * root dotnet /app/out/dotnet-cron.dll >> /var/log/cron.log 2>&1 + +#* * * * * * +#| | | | | | +#| | | | | +-- Year (range: 1900-3000) +#| | | | +---- Day of the Week (range: 1-7, 1 standing for Monday) +#| | | +------ Month of the Year (range: 1-12) +#| | +-------- Day of the Month (range: 1-31) +#| +---------- Hour (range: 0-23) +#+------------ Minute (range: 0-59) +# Cron requires a blank space at the end of the file so leave this here. \ No newline at end of file