Showing posts with label acr. Show all posts
Showing posts with label acr. Show all posts

Az-204 - Prep - How To - Creating a container and access its service in Azure environment with ACR

Hello Team, 

Below commands can be used to create a container with image from ACR and to access the container service.


az login \
--username <> \
--password <> \

az group list | select -p name

# Deploying a container from public registry
az container create \
--resource-group "1-028b2503-playground-sandbox" \
--name psdemo-hello-world-cli-010 \
--dns-name-label psdemo-hello-world-cli-010 \
--image mcr.microsoft.com/azuredocs/aci-helloworld \
--ports 80

# show the container info
az container show --resource-group "1-5e3dbe31-playground-sandbox" \
--name "psdemo-hello-world-cli-009"

# Retrieve the container URL to access it over internet
URL=$(az container show --resource-group '1-5e3dbe31-playground-sandbox' \
--name 'psdemo-hello-world-cli-009' --query ipAddress.fqdn | tr -d '"')
echo "https://$URL"

# Demo 1 - Deploy container from azure container registry
## STEP 0 SET ENVIRONMENT VARIABLES
ACR_NAME="grsr18"
ACR_REGISTRY_ID=$(az acr show --name $ACR_NAME --query id --output tsv)
ACR_LOGINSERVER=$(az acr show --name $ACR_NAME --query loginServer --output tsv)

echo "ACR ID: $ACR_REGISTRY_ID"
echo "ACR_LOGINSERVER $ACR_LOGINSERVER"

## STEP 1 create a service principal and get the password and ID,
## this will allow azure container instances to pull
SP_NAME=acr-service-principal
SP_PASSWD=$(az ad sp create-for-rbac \
--name http://$ACR_NAME-pull \
--scopes $ACR_REGISTRY_ID \
--role acrpull \
--query password \
--output tsv)
SP_APPID=$(az ad sp show --id http://$ACR_NAME-pull --query appId --output tsv)

echo "Servce Princial ID : $SP_APPID"
echo "Service Princial Password $SP_PASSWD"

## STEP 2
az container create \
--resource-group "1-5e3dbe31-playground-sandbox" \
--name "psdemo-webapp-cli" \
--dns-name-label psdemo-webapp-cli \
--ports 80 \
--image $ACR_LOGINSERVER/webappimage:v1 \
--registry-login-server $ACR_LOGINSERVER \
--registry-username $SP_APPID \
--registry-password $SP_PASSWD

# due to permission issues if you cant create service princial
# then you can use your acr login creds to pull image
az container create --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli" --dns-name-label "psdemo-webapp-cli" \
--ports 80 --image $ACR_LOGINSERVER/webappimage:v1 \
--registry-login-server $ACR_LOGINSERVER --registry-username "GRSR18" \
--registry-password "xx=xxxxx=xxxxx+xxxxx"

# get the container status
az container show --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli"
az container show --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli" --query instanceView.state

# get the FQDN to access
url=$(az container show --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli" --query ipAddress.fqdn | tr -d '"')
echo http://$url
curl http://$url

# Get contianer logs
az container logs --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli"

# Delete the running container
az container delete --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli" --yes

# Cleanup from our demos, all ACIs and ACR Deployed in the resource group
az group delete --name "1-028b2503-playground-sandbox" --yes
docker image rm grsr18.azureacr.ip/webappimage:v1
docker image rm webappimage:v1



Az-204 - Prep - How To - Creating Azure Container Registry And Upload/Push docker image into Azure Container Registry (ACR)

 

Hello Everyone, 

This post contains below list of topics

  1. How to create ACR ( Azure Container Registry )
  2. How to login into ACR
  3. How to push Docker images into ACR
  4. How to build docker images with ACR
Github link contains a sample dotnet webapp, which we can build using given instructions. I will share the Github link soon. But you can build your own container image and upload it ACR with same instructions as give below.
# Login
az login --username "xxxx" --password "xxxx"

# Get the resource group name
az group list

# if you want to set subscription
az account set --subscription "SUB NAme"

# create a rg
az group create \
--name psdemo-rg \
--location centralus

# or list existing with

az group list

# Set ACR name , globally unique
ACR_NAME="GRSR810"
az acr create \
--resource-group "1-028b2503-playground-sandbox" \
--name $ACR_NAME \
--sku Standard

# login into ACR
az acr login --name $ACR_NAME

# Get the login server which is used in the image tag
az acr show --name $ACR_NAME --query loginServer
az acr show --name $ACR_NAME --query loginServer --output tsv
ACR_LOGINSERVER=$(az acr show --name $ACR_NAME --query loginServer --output tsv)
echo $ACR_LOGINSERVER

Tag the container image using the login server name.
docker image ls webappimage:v1
docker tag webappimage:v1 $ACR_LOGINSERVER/webappimage:v1
docker image ls $ACR_LOGINSERVER/webappimage:v1
docker image ls

# Push the image into Docker registry
docker push $ACR_LOGINSERVER/webappimage:v1

# Get list of the repositories and images/tags from ACR
az acr repository list --name $ACR_NAME --output table
az acr repository show-tags --name $ACR_NAME --repository webappimage --output table

# By using tasks we can build ACR with tasks.
az acr build --image "webappimage:v1-acr-task" --registry $ACR_NAME .

# 2 images must be there now
az acr repository show-tags --name $ACR_NAME --repository webappimage --output table


Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1

RUN mkdir /app
WORKDIR /app

COPY ./webapp/bin/Release/netcoreapp3.1/publish ./
COPY ./config.sh ./

RUN bash config.sh

EXPOSE 80
ENTRYPOINT [ "dotnet", "webapp.dll" ]

Building dotnet Web application

ls -ls ./webapp

dotnet build ./webapp
dotnet run --project ./webapp
curl http://localhost:5000

# Publish local build
dotnet publish -c Release ./webapp

# build docker image
docker build -t webappimage:v1 .

# Run the container
docker run --name webapp --publish 8080:80 --detach webappimage:v1
curl http://localhost:8080

docker stop webapp
docker rm webapp

docker image ls webappimage:v1