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:
- Copies your project files across and restores them.
- Copies your application files across to the docker image.
- Publishes your application to the out location.
- Creates a new 2.0.0-runtime image.
- Copies your published app to the new image.
- Installs cron into the runtime image.
- Copies scripts and schedule file, removes carrage returns and sets permissions
- Copies environment variables so that cron can access them.
- Runs cron and outputs the contents of the /var/log/cron.log file to the screen.
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