Tutorial: Update a container app deployed from source code
This article demonstrates how to update the container app you created in the previous article, Build and deploy your source code to Azure Container Apps.
If you haven't completed the steps in the previous article, stop here and return to this article once all steps are done.
In this tutorial you:
- Make a code change to your application.
- Push your changes to the container registry with a new tag.
- View the updated app in a browser.
- Query the log stream to view logged messages.
Prerequisites
To complete this project, you need the tools, resources, and container app created in the previous tutorial, Build and deploy from source code to Azure Container Apps.
Setup
If necessary, sign in to Azure from the CLI.
az login
Create environment variables. If your environment variables from the last tutorial still exist in your terminal, you can skip this step.
If you need to recreate the environment variables, you first need to query for the container registry name you created in the last article.
Run the following command to query for the container registry you created in the last tutorial.
az acr list --query "[].{Name:name}" --output table
Once you have your container registry name, replace
<REGISTRY_NAME>
with your registry name and run the following command.RESOURCE_GROUP="my-demo-group" CONTAINER_APP_NAME="my-demo-app" REGISTRY_NAME="<REGISTRY_NAME>"
Update and run your source code.
Replace the contents of
Startup.cs
with the following code.public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app, ILogger<Startup> logger) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { logger.LogInformation("Hello Logger!"); await context.Response.WriteAsync("Hello Logger!"); }); }); } }
This version of the code registers a logger to write information out to the console and the Container Apps log stream.
Build your project in Release configuration.
dotnet build -c Release
Next, run your application to verify your code is implemented correctly.
dotnet run --configuration Release
Build and push the image to a registry
Now that your code is updated, you can push the latest version as a new image to your container registry.
To ensure tag used for your registry is unique, use the following command to create a tag name.
IMAGE_TAG=$(date +%s)
Now you can build and push your new container image to the registry using the following command.
az acr build \
-t $REGISTRY_NAME.azurecr.io/$CONTAINER_APP_NAME:$IMAGE_TAG \
-r $REGISTRY_NAME .
Create a new revision
You can create a new revision of your container app based on the new container image you pushed to your registry.
az containerapp revision copy \
--name $CONTAINER_APP_NAME \
--resource-group $RESOURCE_GROUP \
--image "$REGISTRY_NAME.azurecr.io/$CONTAINER_APP_NAME:$IMAGE_TAG" \
--output none
The revision copy
command creates a new revision of your container app with the specified container image from the registry.
Verify deployment
Now that your application is deployed, you can query for the URL with this command.
az containerapp show \
--name $CONTAINER_APP_NAME \
--resource-group $RESOURCE_GROUP \
--query properties.configuration.ingress.fqdn -o tsv
In a web browser, go to the app's URL. Once the container app is started, it outputs Hello Logger!.
Query log stream
You just saw the output sent to the browser, so now you can use the following command to see the messages being logged in the log stream.
az containerapp logs show \
--name $CONTAINER_APP_NAME \
--resource-group $RESOURCE_GROUP \
--follow
The query returns a response similar to the following example:
{"TimeStamp", "xxxx", "Log": "info: Microsoft.Hosting.Lifetime[0]"}
{"TimeStamp", "xxxx", "Log": "Hosting environment: Production"}
{"TimeStamp", "xxxx", "Log": "info: Microsoft.Hosting.Lifetime[0]"}
{"TimeStamp", "xxxx", "Log": "Content root path: /app"}
{"TimeStamp", "xxxx", "Log": "info: Startup[0]"}
{"TimeStamp", "xxxx", "Log": "Hello Logger!""}
Notice how you can see the message of Hello Logger!
in the stream.
To stop following the stream, you can enter Cmd/Ctrl + C to terminate the messages.
Clean up resources
If you're not going to use the Azure resources created in this tutorial, you can remove them with the following command.
az group delete --name my-demo-group
Tip
Having issues? Let us know on GitHub by opening an issue in the Azure Container Apps repo.
Next steps
Continue on to learn how to connect to services in Azure Container Apps.