LoginSignup
0

More than 1 year has passed since last update.

Azure CLIでACRへDockerイメージをpush、ACIにデプロイする[on Windows]

Last updated at Posted at 2021-02-03

最近AzureやWindows OSを使い始めました。
もともとAWSやGCPで同様のDockerコンテナをデプロイするスクリプトをshellで書いていたので、PowerShellでもやってみました。
(なお、コマンドレットは使用していないのでPowerShellでやる必要はないですが。)

コードはこちら
https://github.com/yolo-kiyoshi/azure_cli_sample/tree/master/script

概要

Azure CLIで以下を実行できるスクリプト

  • ローカルのDockerイメージをbuild、Azure Container RegistoryにDockerイメージをpushする
  • Azure Container InstanceにAzure Container RegistoryのDockerイメージをpull、コンテナを作成する

前提

  • WindowsでPowershellをインストール済み
  • Azure CLIをインストール済み
  • acrpullロールを付与したサービスプリンシパルを作成済み
  • Container Registoryを作成済み

コード

ACRへDockerイメージをpush

.\script\build_and_push.ps1
# read from setting file
$settings = Get-Content .\config.txt | ConvertFrom-StringData

# login
Write-Host "start login..."
az login --service-principal `
    --username $settings.USER_NAME `
    --tenant $settings.TENANT `
    --password $settings.PASSWORD
if ( $LastExitCode -ne 0 )
{
    Write-Host "login into azure failed."
    exit 1
}
else
{
    Write-Host "login completed."
}

# registory_name
$REGISTORY_NAME = $settings.REGISTORY_NAME
# Docker image full name
$image_name_tag = $settings.IMAGE_NAME + ":" + $settings.TAG
$acr_path = "${REGISTORY_NAME}.azurecr.io/${image_name_tag}"

# login into ACR
$is_login = az acr login --name ${REGISTORY_NAME}
if ( $is_login -cne "Login Succeeded" )
{
    Write-Host "login into acr failed."
    exit 1
}
else
{
    Write-Host "login into acr completed."
}

docker build -t $image_name_tag .
if ( $LastExitCode -eq 1 )
{
    Write-Host "docker build failed."
    exit 1
}
else
{
    Write-Host "docker build completed."
}

docker tag $settings.IMAGE_NAME $acr_path

docker push $acr_path
if ( $LastExitCode -eq 1 )
{
    Write-Host "docker push failed."
    exit 1
}
else
{
    Write-Host "docker push ${acr_path} completed."
}

docker rmi -f $acr_path
if ( $LastExitCode -eq 1 )
{
    Write-Host "remove ${acr_path} failed."
    exit 1
}
else
{
    Write-Host "remove ${acr_path} completed."
}

ACIにDockerコンテナをdeploy

deploy.ps1
# read from setting file
$settings = Get-Content .\config.txt | ConvertFrom-StringData

# login
Write-Host "start login..."
az login --service-principal `
    --username $settings.USER_NAME `
    --tenant $settings.TENANT `
    --password $settings.PASSWORD
if ( $LastExitCode -ne 0 )
{
    Write-Host "login into azure failed."
    exit 1
}
else
{
    Write-Host "login completed."
}

# registory_name
$REGISTORY_NAME = $settings.REGISTORY_NAME
# Docker image full name
$image_name_tag = $settings.IMAGE_NAME + ":" + $settings.TAG
# Container Registory login server
$ACR_LOGIN_SERVER = "${REGISTORY_NAME}.azurecr.io"

Write-Host "start create container..."
# https://docs.microsoft.com/en-us/cli/azure/container?view=azure-cli-latest#az_container_create
az container create `
    --resource-group ${settings}.RESOURCE_GROUP `
    --name ${settings}.CONATINER_NAME `
    --image "${ACR_LOGIN_SERVER}/${image_name_tag}" `
    --registry-login-server $ACR_LOGIN_SERVER `
    --registry-username $settings.USER_NAME `
    --registry-password $settings.PASSWORD `
    --restart-policy Never
if ( $LastExitCode -ne 0 )
{
    Write-Host "create container failed."
    exit 1
}
else
{
    Write-Host "create container completed."
}

利用方法

.\config.txtに設定情報を記載し、以下を実行する。

Dockerイメージのbuild、ACRへのイメージのpush

.\script\build_and_push.ps1

ACIにコンテナをdeploy

.\script\deploy.ps1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0