Prerequisites:
- An Azure subscription
- Basic understanding of containerization and Docker
- A .NET Core 3.1 or later project set up with a BackgroundService
- A container image (e.g., your .NET Core project) stored in a registry like Docker Hub
Step 1: Create a new Container App
- Log in to the Azure portal at portal.azure.com.
- Click on Create a resource > Container Apps.
- Fill in the required details:
- Name: Give your container app a name (e.g., "my-container-app").
- Subscription: Select your Azure subscription.
- Resource group: Create a new resource group or use an existing one.
- Click on Review + create.
Step 2: Choose the Container App type
- On the next screen, choose the type of container app you want to create:
- App: A stateless web application (e.g., a Node.js server).
- Job: A short-running task or batch job.
- Queue: A message-driven queue (not relevant for this example).
- Select Job.
Step 3: Configure the Job
- Fill in the required details:
- Container settings:
- Image URL: The URL of your container image in a registry like Docker Hub.
- Container port: Since we're not exposing any ports for this BackgroundService, you can leave it blank or set
to 0.
- Add any additional configurations, such as:
- Environment variables
- CPU and memory settings
Step 4: Configure the .NET Background Worker Service
- In the Azure portal, navigate to your Container App's Configuration section.
- Click on Add configuration > **AppSettings`.
- Add a new key-value pair:
- Key:
Microsoft.Azure.WorkerRole.Extensions.Configuration
- Value:
true
- This will enable the BackgroundService to read from Azure configuration.
Step 5: Create a Trigger for the Job
- Click on the Triggers tab.
- Click on Add trigger.
- Select Scheduled trigger.
- Configure the scheduled trigger:
- Schedule: Choose when you want the job to run (e.g., every hour, daily).
- Time zone: Select a time zone for the schedule.
Step 6: Create the Job
- Click on Review + create to review your configuration.
- Click on Create to deploy the job.
Verification
Once the job is created, you can verify that it's running by checking the container app's logs in the Azure
portal. You can also use Azure CLI or other tools to interact with the job.
Some additional things to note:
- Since this is a BackgroundService, it will run as a long-running task.
- Make sure your .NET project has the necessary dependencies for Azure Container Apps (e.g.,
Microsoft.Azure.WorkerRole.Extensions.Configuration
).
- You can have multiple triggers for a single job (e.g., both scheduled and queue-based triggers).