Added support for environment variables

This commit is contained in:
Alex Hyett 2017-07-07 17:00:38 +01:00 committed by GitHub
parent 571e0f3282
commit 1f2d4c67d4
7 changed files with 55 additions and 16 deletions

View file

@ -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 # Install Cron
RUN apt-get update -qq && apt-get -y install cron -qq --force-yes 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 # Set up schedule
ADD schedule /etc/cron.d/schedule ADD schedule /etc/cron.d/schedule
RUN chmod 0644 /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 touch /var/log/cron.log
RUN chmod 0666 /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 required for tail command
VOLUME /var/log VOLUME /var/log
# Run Cron # Run Cron
CMD /usr/sbin/cron && tail -f /var/log/cron.log CMD /app/export_env.sh && /usr/sbin/cron && tail -f /var/log/cron.log

View file

@ -7,6 +7,7 @@ namespace test_dotnet
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine($"This program should be running on a schedule: {DateTime.Now}"); Console.WriteLine($"This program should be running on a schedule: {DateTime.Now}");
Console.WriteLine($"TEST_ENV={Environment.GetEnvironmentVariable("TEST_ENV")}");
} }
} }
} }

View file

@ -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 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: The docker file attached does the following:
1. Installs cron on the standard microsoft/dotnet:1.1.1-sdk image. 1. Installs cron on the standard microsoft/dotnet:1.1.1-sdk image.
2. Copies your application files across to the docker image. 2. Copies your application files across to the docker image.
3. Copies the schedule file across to the docker image. 3. Copies the schedule file across to the docker image.
4. Restores and Publishes your dotnet core 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 ## 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: Then run:
`docker-compose up` `docker-compose up`

View file

@ -4,3 +4,5 @@ services:
console: console:
build: . build: .
container_name: recurring-console container_name: recurring-console
environment:
- TEST_ENV=TestValue3

5
export_env.sh Normal file
View file

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

7
run_app.sh Normal file
View file

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

View file

@ -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
#* * * * * * #* * * * * *
#| | | | | | #| | | | | |