diff --git a/Dockerfile b/Dockerfile index fc61780..15934f5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,28 @@ -FROM microsoft/dotnet:1.1.1-sdk +FROM microsoft/dotnet:1.1.1-sdk AS build -ARG BUILDCONFIG=DEBUG +ARG BUILDCONFIG=RELEASE + +# Copy project files to avoid restoring packages if they haven't changed +COPY *.csproj ./build/ +RUN dotnet restore ./build/ + +COPY . ./build/ +WORKDIR /build/ +RUN dotnet publish -c $BUILDCONFIG -o out + +# build runtime image +FROM microsoft/dotnet:1.1.1-runtime +WORKDIR /app +COPY --from=build /build/out ./ + +# Add export environment variable script +COPY export_env.sh . +COPY run_app.sh . +RUN chmod +x export_env.sh run_app.sh # 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 @@ -17,12 +31,8 @@ RUN chmod 0644 /etc/cron.d/schedule 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 +CMD /app/export_env.sh && /usr/sbin/cron && tail -f /var/log/cron.log \ No newline at end of file diff --git a/Program.cs b/Program.cs index 65043a7..bbfdc71 100644 --- a/Program.cs +++ b/Program.cs @@ -7,6 +7,7 @@ namespace test_dotnet static void Main(string[] args) { Console.WriteLine($"This program should be running on a schedule: {DateTime.Now}"); + Console.WriteLine($"TEST_ENV={Environment.GetEnvironmentVariable("TEST_ENV")}"); } } } diff --git a/README.md b/README.md index 4e26d42..6750795 100644 --- a/README.md +++ b/README.md @@ -4,18 +4,32 @@ This is an example project showing you how to run a console app in a schedule in 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. 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. +5. Copies environment variables so that cron can access them. +6. 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). +To use this within your own dotnet core console app you will need the following files: -You need to modify the schedule file to reference the dll of your console app. +* 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` +`docker-compose up` \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index ccb3864..854507a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,3 +4,5 @@ services: console: build: . container_name: recurring-console + environment: + - TEST_ENV=TestValue3 \ No newline at end of file diff --git a/export_env.sh b/export_env.sh new file mode 100644 index 0000000..0371539 --- /dev/null +++ b/export_env.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +echo '#!/bin/bash' > /app/set_env.sh +printenv | sed '/^affinity:container/ d' | sed -r 's/^([a-zA-Z_]+[a-zA-Z0-9_:-]*)=(.*)$/export \1="\2"/g' >> /app/set_env.sh +chmod +x /app/set_env.sh \ No newline at end of file diff --git a/run_app.sh b/run_app.sh new file mode 100644 index 0000000..31c5b83 --- /dev/null +++ b/run_app.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# Set environment variables copied from container +source /app/set_env.sh; + +# Run your dotnet console app +dotnet /app/dotnet-cron.dll \ No newline at end of file diff --git a/schedule b/schedule index cc2cb6b..f1c6641 100644 --- a/schedule +++ b/schedule @@ -1,4 +1,4 @@ -* * * * * root dotnet /app/out/dotnet-cron.dll >> /var/log/cron.log 2>&1 +* * * * * root /app/run_app.sh >> /var/log/cron.log 2>&1 #* * * * * * #| | | | | |