Skip to content

Files

Latest commit

author
Christopher McClister
May 25, 2022
f315724 · May 25, 2022

History

History
355 lines (229 loc) · 11 KB

quickstart-code-to-cloud.md

File metadata and controls

355 lines (229 loc) · 11 KB
title description services author ms.service ms.topic ms.date ms.author zone_pivot_groups
Quickstart: Deploy your code to Azure Container Apps
Code to cloud deploying your application to Azure Container Apps
container-apps
craigshoemaker
container-apps
quickstart
05/11/2022
cshoe
container-apps-image-build-type

Quickstart: Deploy your code to Azure Container Apps

This article demonstrates how to build and deploy a microservice to Azure Container Apps from a source repository using the programming language of your choice.

This quickstart is the first in a series of articles that walk you through how to use core capabilities within Azure Container Apps. The first step is to create a back end web API service that returns a static collection of music albums.

The following screenshot shows the output from the album API deployed in this quickstart.

:::image type="content" source="media/quickstart-code-to-cloud/azure-container-apps-album-api.png" alt-text="Screenshot of response from albums API endpoint.":::

Prerequisites

To complete this project, you'll need the following items:

::: zone pivot="acr-remote"

Requirement Instructions
Azure account If you don't have one, create an account for free. You need the Contributor or Owner permission on the Azure subscription to proceed.

Refer to Assign Azure roles using the Azure portal for details.
GitHub Account Sign up for free.
git Install git
Azure CLI Install the Azure CLI.

::: zone-end

::: zone pivot="docker-local"

Requirement Instructions
Azure account If you don't have one, create an account for free. You need the Contributor or Owner permission on the Azure subscription to proceed. Refer to Assign Azure roles using the Azure portal for details.
GitHub Account Sign up for free.
git Install git
Azure CLI Install the Azure CLI.
Docker Desktop Docker provides installers that configure the Docker environment on macOS, Windows, and Linux.

From your command prompt, type docker to ensure Docker is running.

::: zone-end

[!INCLUDE container-apps-setup-cli-only.md]

Now that your Azure CLI setup is complete, you can define the environment variables that are used throughout this article.

[!INCLUDE container-apps-code-to-cloud-setup.md]

Prepare the GitHub repository

Navigate to the repository for your preferred language and fork the repository.

Select the Fork button at the top of the album API repo to fork the repo to your account.

Now you can clone your fork of the sample repository.

Use the following git command to clone your forked repo into the code-to-cloud folder:

git clone https://github.com/$GITHUB_USERNAME/containerapps-albumapi-csharp.git code-to-cloud

Select the Fork button at the top of the album API repo to fork the repo to your account.

Now you can clone your fork of the sample repository.

Use the following git command to clone your forked repo into the code-to-cloud folder:

git clone https://github.com/$GITHUB_USERNAME/containerapps-albumapi-go.git code-to-cloud

Select the Fork button at the top of the album API repo to fork the repo to your account.

Now you can clone your fork of the sample repository.

Use the following git command to clone your forked repo into the code-to-cloud folder:

git clone https://github.com/$GITHUB_USERNAME/containerapps-albumapi-javascript.git code-to-cloud

Select the Fork button at the top of the album API repo to fork the repo to your account.

Now you can clone your fork of the sample repository.

Use the following git command to clone your forked repo into the code-to-cloud folder:

git clone https://github.com/$GITHUB_USERNAME/containerapps-albumapi-python.git code-to-cloud

Next, change the directory into the root of the cloned repo.

cd code-to-cloud/src

Create an Azure Resource Group

Create a resource group to organize the services related to your container app deployment.

az group create \
  --name $RESOURCE_GROUP \
  --location "$LOCATION"
az group create `
  --name $RESOURCE_GROUP `
  --location "$LOCATION"

Create an Azure Container Registry

Next, create an Azure Container Registry (ACR) instance in your resource group to store the album API container image once it's built.

az acr create \
  --resource-group $RESOURCE_GROUP \
  --name $ACR_NAME \
  --sku Basic \
  --admin-enabled true
az acr create `
  --resource-group $RESOURCE_GROUP `
  --name $ACR_NAME `
  --sku Basic `
  --admin-enabled true

::: zone pivot="acr-remote"

Build your application

With ACR tasks, you can build and push the docker image for the album API without installing Docker locally.

Build the container with ACR

Run the following command to initiate the image build and push process using ACR. The . at the end of the command represents the docker build context, meaning this command should be run within the src folder where the Dockerfile is located.

az acr build --registry $ACR_NAME --image $API_NAME .
az acr build --registry $ACR_NAME --image $API_NAME .

Output from the az acr build command shows the upload progress of the source code to Azure and the details of the docker build and docker push operations.

::: zone-end

::: zone pivot="docker-local"

Build your application

The following steps, demonstrate how to build your container image locally using Docker and push the image to the new container registry.

Build the container with Docker

The following command builds a container image for the album API and tags it with the fully qualified name of the ACR login server. The . at the end of the command represents the docker build context, meaning this command should be run within the src folder where the Dockerfile is located.

docker build --tag $ACR_NAME.azurecr.io/$API_NAME .
docker build --tag "$ACR_NAME.azurecr.io/$API_NAME" .

Push the image to your container registry

First, sign in to your Azure Container Registry.

az acr login --name $ACR_NAME
az acr login --name $ACR_NAME

Now, push the image to your registry.

docker push $ACR_NAME.azurecr.io/$API_NAME
docker push "$ACR_NAME.azurecr.io/$API_NAME"

::: zone-end

Create a Container Apps environment

The Azure Container Apps environment acts as a secure boundary around a group of container apps.

Create the Container Apps environment using the following command.

az containerapp env create \
  --name $ENVIRONMENT \
  --resource-group $RESOURCE_GROUP \
  --location "$LOCATION"
az containerapp env create `
  --name $ENVIRONMENT `
  --resource-group $RESOURCE_GROUP `
  --location $LOCATION

Deploy your image to a container app

Now that you have an environment created, you can create and deploy your container app with the az containerapp create command.

Create and deploy your container app with the following command.

az containerapp create \
  --name $API_NAME \
  --resource-group $RESOURCE_GROUP \
  --environment $ENVIRONMENT \
  --image $ACR_NAME.azurecr.io/$API_NAME \
  --target-port 3500 \
  --ingress 'external' \
  --registry-server $ACR_NAME.azurecr.io \
  --query configuration.ingress.fqdn
az containerapp create `
  --name $API_NAME `
  --resource-group $RESOURCE_GROUP `
  --environment $ENVIRONMENT `
  --image "$ACR_NAME.azurecr.io/$API_NAME" `
  --target-port 3500 `
  --ingress 'external' `
  --registry-server "$ACR_NAME.azurecr.io" `
  --query configuration.ingress.fqdn

  • By setting --ingress to external, your container app will be accessible from the public internet.

  • The target-port is set to 3500 to match the port the that the container is listing to for requests.

  • Without a query property, the call to az containerapp create returns a JSON response that includes a rich set of details about the application. By adding a query, this command filters the response down to just the FQDN.

Verify deployment

The az containerapp create command returns the fully qualified domain name (FQDN) for the container app. Copy the FQDN to a web browser.

From your web browser, navigate to the /albums endpoint of the FQDN.

:::image type="content" source="media/quickstart-code-to-cloud/azure-container-apps-album-api.png" alt-text="Screenshot of response from albums API endpoint.":::

Clean up resources

If you're not going to continue on to the Communication between microservices tutorial, you can remove the Azure resources created during this quickstart. Run the following command to delete the resource group along with all the resources created in this quickstart.

az group delete --name $RESOURCE_GROUP
az group delete --name $RESOURCE_GROUP

Tip

Having issues? Let us know on GitHub by opening an issue in the Azure Container Apps repo.

Next steps

This quickstart is the entrypoint for a set of progressive tutorials that showcase the various features within Azure Container Apps. Continue on to learn how to enable communication from a web front end that calls the API you deployed in this article.

[!div class="nextstepaction"] Tutorial: Communication between microservices