How to run a recurring .NET Core console app in a docker container using cron
Find a file
2018-06-22 11:41:47 +01:00
.dockerignore Added .dockerignore file 2017-07-07 17:51:50 +01:00
.gitignore Upgrade to .Net Core 2.0.0 and improve regex 2017-09-26 22:09:31 +01:00
appsettings.json Upgrade to .Net Core 2.0.0 and improve regex 2017-09-26 22:09:31 +01:00
ConsoleSettings.cs Upgrade to .Net Core 2.0.0 and improve regex 2017-09-26 22:09:31 +01:00
docker-compose.yml Removed stripping out code of colons as not required 2017-09-26 22:22:01 +01:00
Dockerfile Upgrade to .Net Core 2.0.0 and improve regex 2017-09-26 22:09:31 +01:00
dotnet-cron.csproj Upgrade to .Net Core 2.0.0 and improve regex 2017-09-26 22:09:31 +01:00
export_env.sh Removed stripping out code of colons as not required 2017-09-26 22:22:01 +01:00
LICENSE Initial commit 2017-07-06 10:03:16 +01:00
Program.cs Add configuration settings 2017-09-26 22:10:02 +01:00
README.md Updated Readme.me with blog link 2018-06-22 11:41:47 +01:00
run_app.sh Added support for environment variables 2017-07-07 17:00:38 +01:00
schedule Added support for environment variables 2017-07-07 17:00:38 +01:00

.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.

For more information see a full breakdown on my blog: https://www.alexhyett.com/scheduled-dotnet-core-console/

The docker image makes use of cron which is a unix utility for running commands on a schedule.

The issue I came across with running console apps in cron is that the environment variables aren't visible to cron jobs. You have to specify the environment variables up front before running your job.

However, the whole point of Docker is not to have a hardcoded values which are likely to change between environments.

You therefore need to copy the environment variables to a script when your container starts and then set up your environment as part of the cron script.

The docker file attached does the following:

  1. Copies your project files across and restores them.
  2. Copies your application files across to the docker image.
  3. Publishes your application to the out location.
  4. Creates a new 2.0.0-runtime image.
  5. Copies your published app to the new image.
  6. Installs cron into the runtime image.
  7. Copies scripts and schedule file, removes carrage returns and sets permissions
  8. Copies environment variables so that cron can access them.
  9. 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 following files:

  • Dockerfile
  • schedule
  • export_env.sh
  • run_app.sh
  • .dockerignore
  • docker-compose.yml

You need to modify the run_app.sh file to reference the dll of your console app and the schedule file if you want it to run on a different schedule other than once a minute.

Then run:

docker-compose up