Skip to content

Running console/batch application in docker container using Azure container instances and logic apps

Notifications You must be signed in to change notification settings

TharinduK/Azure-Console-App-in-Container-Instance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Running .net console application on Azure container instance

We sometimes have the need to use a batch (console) application to perform some computational tasks. These tasks may run on a schedule, however may not run often enough to justify setting up a dedicated deployment/run pipeline. I would like to run these processors on serverless cloud infrastructure so that you are only billed when service is utilized, and use low-code options for setup.

Below I have described my experiment on how I tried to achieve this objective using Docker, Azure Container Instances service, and Azure Logic Apps.

High-level process overview

Console App on CI Process

High-level steps

  1. Create console app
  2. Add docker support
  3. Setup Azure infrastructure
  4. Build and load image to registry
  5. Trigger logic app

Experiment

Prerequisites

Create console app

  1. Create .net application using vs code

Get supported project templates

dotnet new 

Create new console project (and configure git repo)

# dotnet new <short name> -n <project name> -o <project folder>
dotnet new console -n helloConsole -o App
cd App
dotnet new gitignore
git init

Open project in vs code

code .
  1. Update code (Program.cs) to crate sample application that takes value from environment variable
Console.WriteLine("Start long running app");

var counter = 0;
var maxCount = Environment.GetEnvironmentVariable("max_count");
var max = string.IsNullOrEmpty(maxCount) ?-1: Convert.ToInt32(maxCount);
while (max == -1 || counter < max)
{
    Console.WriteLine($"Counter: {++counter}");
    await Task.Delay(TimeSpan.FromMilliseconds(1_000));
}

Console.WriteLine("End long running app");
  1. Add debugging files
    • "Run and Debug" extension >> "create a launch.json file > .NET 5+ and .NET Core" Add debugging file
    • "Run and Debug" extension >> .NET Core Launch

Add docker support

  1. Add docker support using docker extension
    • Ctrl+Shift+P >> Add docker file.. Add docker file

    • Select project type Add docker file

    • Select OS Add docker file

    • No docker compose (single service) Add docker file

    • Docker file is created Add docker file

Example of the docker file thats generated

FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base
WORKDIR /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["helloConsole.csproj", "./"]
RUN dotnet restore "helloConsole.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "helloConsole.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "helloConsole.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "helloConsole.dll"]
  1. Debug project in docker
  • "Run and Debug" extension >> "Docker .NET Core Launch Add docker file

Setup Azure infrastructure

Deploy to Azure

I used Azure portal to setup resources, however when I had a working version I created bicep and arm template to make setup simpler. Use the "Deploy to Azure" button to deploy an ARM Template to seutp the following resources:

  1. Azure Container Registry (if required)
  2. Azure logic app

Note: I have not been able to get logic app connection to the container registry working with biceps. Currently you would need to add a new connection using your credentials.

Logic app

Logic app has the following workflow

  1. Triggered by HTTP request
  2. Set environment variables
  3. Create Azure Container instance (pull image from container registry and run container) (options )
  4. Wait until complete
  5. Get container log
  6. Delete container instance

Workflow overview Logic App High Level

Crate container instance

create ci

create ci

Build and load image to registry

  1. Build a docker image with docker file in the current directory (.)
# Docker build –t [image name]:[version] -f [docker file location] [path]
image=helloconsole
ver=latest
docker build -t $image:$ver -f Dockerfile  .
  1. Setup script variables
registry=demotkcr
image=helloconsole
ver=latest

sub=[subscription id]
rg=demotk-rg
  1. Tag local docker image so it can be pushed to docker registry
#docker tag source_image[:tag] target_image[:tag]
docker tag $image:latest $registry.azurecr.io/$image:$ver
  1. log in to Azure
az login
  1. Set context to correct subscription
az account set -s $sub
az account list -o table
  1. Get admin password for ACR and log in to container registry
un=$(az acr credential show -n $registry -g $rg --query username -o tsv)
pw=$(az acr credential show -n $registry -g $rg --query passwords[0].value -o tsv)
docker login $registry.azurecr.io -u $un -p $pw
  1. Push image to registry
# docker push [docker registry name]/[Image name]:[version]
docker push $registry.azurecr.io/$image:$ver

Trigger logic app

Log in to Azure portal, select logic app, and trigger a test run.

Findings

The process worked well for jobs that are running less than 10 minutes.

For jobs that are running longer the step to delete the container instance did not run, however the container was stopped and we were not billed by Azure once it was stopped

Next steps

  • Investigate how to set up connections to container registry through biceps
  • Investigate how to cleanup container instances for longer running processors

Refernece

About

Running console/batch application in docker container using Azure container instances and logic apps

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published