Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Deploy Azure web job on Docker
Vedant
Vedant

Posted on

     

Deploy Azure web job on Docker

What is WebJobs SDK?

The Azure WebJobs SDK is a framework that simplifies the task of writing background processing code that runs in Azure WebJobs. It includes a declarative binding and trigger system that works with Azure Storage Blobs, Queues and Tables as well as Service Bus. The binding system makes it incredibly easy to write code that reads or writes Azure Storage objects. The trigger system automatically invokes a function in your code whenever any new data is received in a queue or blob.

The version 3.x of the WebJobs SDK supports both .NET Core and .NET Framework console apps. This opens all sorts of possibilities of hosting it apart from App Service. In this article I'll walk you through process of hosting the Web Job on Docker.

Create Web job using Web Job SDK 3

Let's create simple queue triggered web job which will print the message received in the azure queue.

  1. open a command prompt and enter the below command.
dotnet new console-o"ContainerizedWebJob"cdContainerizedWebJob
Enter fullscreen modeExit fullscreen mode
  1. Install the latest stable 3.x versions of the following NuGet packages.
    • Microsoft.Azure.WebJobs
    • Microsoft.Azure.WebJobs.Extensions

Here are the commands:

dotnet add package Microsoft.Azure.WebJobs--version 3.0.11dotnet add package Microsoft.Azure.WebJobs.Extensions--version 3.0.2
Enter fullscreen modeExit fullscreen mode
  1. We will need console logging using which we will print the recieved message from the queue on the console. Install the latest version of belo packages
dotnet add package Microsoft.Extensions.Logging--version 2.2.0dotnet add package Microsoft.Extensions.Logging.Console--version 2.2.0
Enter fullscreen modeExit fullscreen mode
  1. Install storage binding extension Starting with version 3.x, you must explicitly install the Storage binding extension required by the WebJobs SDK. In prior versions, the Storage bindings were included in the SDK. Install latest version of below package
dotnet add package Microsoft.Azure.WebJobs.Extensions.Storage--version 3.0.7
Enter fullscreen modeExit fullscreen mode
  1. Now we will create the host and configure it.Add program.cs file and replace the content of with below.
usingMicrosoft.Extensions.Hosting;usingMicrosoft.Extensions.Logging;namespaceContainerizedWebJob{classProgram{staticvoidMain(string[]args){varbuilder=newHostBuilder();builder.UseEnvironment(EnvironmentName.Development);builder.ConfigureWebJobs(b=>{b.AddAzureStorageCoreServices();b.AddAzureStorage();});builder.ConfigureLogging((context,b)=>{b.AddConsole();});varhost=builder.Build();using(host){host.Run();}}}}
Enter fullscreen modeExit fullscreen mode

AddFunctions.cs file which will have the functions which will process the queue message.

usingMicrosoft.Azure.WebJobs;usingMicrosoft.Extensions.Logging;namespaceContainerizedWebJob{publicclassFunctions{publicstaticvoidProcessQueueMessage([QueueTrigger("message-queue")]stringmessage,ILoggerlogger){logger.LogInformation(message);}}}
Enter fullscreen modeExit fullscreen mode

Add appsettings.json

{"AzureWebJobsStorage":"UseDevelopmentStorage=true"}
Enter fullscreen modeExit fullscreen mode
  1. ModifyContainerizedWebJob.csproj and add new<ItemGroup>.
<ItemGroup><NoneUpdate="appsettings.json"><CopyToOutputDirectory>Always</CopyToOutputDirectory></None></ItemGroup>
Enter fullscreen modeExit fullscreen mode
  1. Lets run an test it.
dotnet builddotnet run
Enter fullscreen modeExit fullscreen mode

Create a queue namedmessage-queue add the message.

Deploy Web job on docker.

To deploy this application on docker we need to specify docker configuration. AddDockerfile at the root of the application.

FROM mcr.microsoft.com/dotnet/core/runtime:2.2WORKDIR /appCOPY ./bin/Debug/netcoreapp2.2/publish .ENTRYPOINT [ "dotnet", "ContainerizedWebJob.dll" ]
Enter fullscreen modeExit fullscreen mode

Before we deploy this app on docker we need to make changes to ourappsettings.json file as below.

{"AzureWebJobsStorage":"UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://host.docker.internal"}
Enter fullscreen modeExit fullscreen mode

Note: TheAzure Storage Emulator is bound in a local-only network configuration andDocker for Windows runs in aVM usingHyper-V. Hence to access the emulator running on host (Windows) we need to use the proxyhttp://host.docker.internal instead ofhttp://127.0.0.1.

Now that we are done with all the configuration we shall create and run the docker image. To create a docker image, run the below command where you have put theDockerfile.

docker build-t containerized-web-job
Enter fullscreen modeExit fullscreen mode

Once the image is create we shall run it using below command.

docker run-it--rm containerized-web-job
Enter fullscreen modeExit fullscreen mode

Note: above command will delete the container as soon as we stop the web job.

You can find source codehere.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi there 👋, I'm Vedant [😄 Pronouns: vai-daant]. I’m currently working on Notion.Net SDK. Ask me about .Net Core, DevOps, C#, Docker, ReactJSFun fact: I can touch my nose with my tongue
  • Work
    Software Engineer
  • Joined

More fromVedant

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp