Let's move the container app with Azure 1

Example of container application application

Virtualization technology has been virtualization of a server using a hyper visor (software for creating a virtual machine (VM)) for some time, but this is also a virtualization of OS and middleware. Therefore, it is common to install or deploy multiple middleware and applications on one virtual server so that the resources assigned to the virtual server can be used effectively. On the other hand, in the case of container virtualization, the container is executed as one process of the OS, so the use of resources can be reduced lightly. In order to take advantage of this feature, it is recommended to create a container so that only a single application and execution environment are included. This is the same as the design philosophy of micro service architecture that divides applications on a functional unit, so there are many cases where container applications are used to build microservices.

About Docker

A container execution engine is required as a foundation for running a container application.The most famous container execution engine is Docker.In Docker, the container application is called "Docker container" and executes it.The Docker container is a process that is launched based on an image file built in a format called "Docker image".The Docker image is built according to the procedure described in the file "Dockerfile".

Container -related services provided by Azure

Azure provides several services that support the creation, management and execution of container applications.The following are typical services related to container.

・Azure Container Registry(ACR) Azure上でホスティングされたプライベートなDockerレジストリです。DockerレジストリはDockerイメージをバージョンごとに管理することのできる保管庫のことで、DockerレジストリにあるDockerイメージを取得(プル)したり、自分が作成したDockerイメージを登録(プッシュ)することができます。パブリックなDockerレジストリとしてはDocker Hubがあり、様々なミドルウェアやツールのDockerイメージが公開されています。ACRはプライベートなDockerレジストリのため、ビジネスに特化したアプリケーションのようにセキュアな情報を含むDockerイメージを範囲を限定して公開することができるようになります。

・Azure Container Instances(ACI) Azure上でコンテナ化アプリケーションを実行する方法のひとつが、Azure Container Instances(ACI)の利用です。ACIを利用することで自前でVMなどのサーバを構築せずに、サーバレスでコンテナを実行することができます。ACIは比較的単純なアプリケーションやバッチ処理など、単一のコンテナでアプリケーションを実行することを想定したサービスとなっています。

・Web App for Containers(App Service) Web App for ContainersはAzure App Serviceの利用形態のひとつで、App Service上にWebアプリケーション用途のDockerコンテナをデプロイすることができます。App Serviceのリソースと知識を使ってコンテナ化アプリケーションを実行することができるので、App Serviceの利用経験がある場合は比較的容易に運用をすることができます。

・Azure Kubernetes Service(AKS) コンテナ化アプリケーションのデプロイやスケーリングなどの管理を自動化するためのプラットフォームであるKubernetesをAzure上で利用できるのがAzure Kubernetes Service(AKS)です。Kubernetesは複数のコンテナを協調させてアプリケーションを構築する場合に有用で、その役割からコンテナオーケストレーションエンジンと呼ばれています。Kubernetesは任意の環境に構築して利用することができますが、AKSを利用することでKubernetesの主要なコンポーネントをAzureがホスティングし管理してくれるため、煩わしい保守を軽減することができます。

・Azure Container Apps Azure Container Appsもサーバレスなコンテナ実行のためのサービスで、Webアプリケーションやバッチアプリケーションを実行することができます。Azure Container Appsもサービスの基盤としてKubernetesを使用していますが、KubernetesのAPIを直接使用できないようになっているため開発者はアプリケーションの実装に集中できるようになっています。そのためAKSに比べてKubernetes関連の設定の自由度は低いですが、気軽にマイクロサービスを構築したい場合に選択肢として検討する価値があります。なおAzure Container Appsは「Microsoft Ignite 2021」で発表された新しいサービスで、執筆時点ではプレビュー版として提供されています。

Register the container image in Azure Container Registry

From now on, I will explain how to use Azure Container Registry (ACR).We actually create a container registry on Azure and perform push (upload) and pull (download) with Docker image.There are two main ways to register a docker image in the container registry.The first is to push the Docker image built on a local machine or build machine to the container registry.The second is to use ACR tasks, ACR function, create a Docker image on Azure and push it to the container registry.This time, I will explain the procedure for registering the Docker image in the former method.

Creating a container registry

First, create a container registry.Log in to the Azure portal, select the "Container Registry" displayed in the search bar at the top of the screen.The container registry list screen is displayed. Select the "Create" button at the top left of the screen or the "Create Container Registry" button at the bottom of the screen.

When the new creation screen of the container registry is displayed, enter the contents. Select any "subscription" and "resource group". In the "registry name", enter the name used for the container registry to be created. The registry name must be unique in ACR. In "Place", specify a region to place container registries. "SKU" refers to a container registry price plan. The storage size of the container registry and the presence or absence of GEO replication varies depending on the SKU. This time, select the lowest "Basic" plan for development and verification. If SKU is a Basic plan, select the "Confirmation and Create" button because there is no item that can change the settings on the "Network" and "Encryption" tabs.

Transition to the confirmation screen, and if there is no error in the input content, select the "Create" button to start creating a container registry.

After waiting for a while, the creation of the container registry will be completed.Once you're done, you will be able to register the Docker image in the container registry.

Creating a Docker image

From now on, we will create a container image (Docker image) for pushing the container registry using Docker. "Docker Desktop" is required to operate various containers using the Docker command, such as creating a Docker image. Docker Desktop can be used for free for personal use, so if the installation is not yet, install it from the [Docker Desktop installation page. Once you have a Docker Desktop, prepare a Docker image to push to the container registry. To create a Docker image, create a script file for building a Docker image called "Dockerfile". Build a Docker image on a local machine based on Dockerfile and push it to ACR. First, create a new directory, create a file named "Dockerfile" in it, and describe the following line.

Create a Docker image that outputs Hello World (Dockerfile)

FROM hello-world:latest

Dockerfile specifies a command called an instruction at the beginning of the line, and describes the content that is the argument of the order.The above Dockerfile uses the "from" instruction.The from instruction specifies the base of the built Docker image.This time, it is specified as a base image of the image of "Hello-World", the official image provided by Docker.The ": Latest" part is called "tag" in Docker, and is a description that specifies the image version.If you specify Latest, you can refer to the latest version.After creating Dockerfile, build the Docker image.Execute the following command in the terminal.

Azureでコンテナ化アプリを動かしてみよう その1

Docker image build

$ cd $ docker build -t zerokara-sample:0.1 .

You can build a Docker image from Dockerfile by using the "Build" command, which is the subcommand of the Docker command.You can specify the name and tag (version) of the image with the "-T" option.This time, the image name "ZEROKARA-SAMPLE" and "0.The tag "1" is specified.The last period of the command specifies the existence of Dockerfile in a relative path.Currently, the command is running in a directory with Dockerfile, so it represents the current directory..Is specified.

After executing the Docker Build command, make sure that the "Docker Images" command is displayed in the list.

Confirmation of Docker image

$ docker imagesREPOSITORY TAG IMAGE ID CREATEDSIZEzerokara-samplelatest bb31f47d8cce2 months ago13.3kB

By executing the Docker Images command, you can display a list of Docker images in the current machine.As mentioned above, the build has been successful if the "Zerokara-Sample" image was displayed earlier in the "Repository" column.

Start the Docker container

Next, based on the Docker image created, try to start the container.

Start the container from the created Docker image

$ docker run zerokara-sample:0.1Hello from Docker!This message shows that your installation appears to be working correctly.・・・中略

By executing the "Docker Run" command, you can start the container from the Docker image.Specify the Docker image name and tag name for the command argument.As a result of launching the container, the message "Hello from Docker!" Was displayed on the standard output.This indicates that the application code implemented in the base image "Hello-World" was executed.Since the Docker image is ready, the next step is to push the image to the container registry.

Push the Docker image to ACR

To push the Docker image to the ACR container registry, use the Azure CLI and Docker command in the terminal.First, connect to the ACR container registry created earlier.

Log in to the container registry

$ az login・・・中略$ az acr login --name zerokaraLogin Succeeded

First, execute the Azure CLI "Az Login" command to log in to Azure on the terminal. In the case of unrogged in Azure, the browser is displayed here and the Azure authentication screen is displayed, so perform authentication processing. If the browser authentication succeeds, JSON of the Azure account information will be output to the terminal. Next, log in to ACR container registry. By logging in to the container registry, you will be able to pull and push the image. Execute by specifying the registry name of the container registry created earlier with the "--name" option in the "AZ ACR Login" command. After the command is executed, the login is successful if it is output with "Login Sukceeded". After completing the login to the container registry, push the Docker image to the registry. In order to push the Docker image to the container registry, you must first change the name of the image to the container registry. By using the "Docker Tag" command, you can give another image name to the Docker image. The first argument following "Docker Tag" specifies the name and tag of the image that is the reference source. Specifies the image name and tag of the reference destination for the second argument. Before the image name of the reference, you can push the container registry by putting the host name and slash of the container registry of the push destination. The host name of the container registry is "Container registry name + azurecr" for ACR..It will be "IO".

Give another name to Docker image

$ docker tag zerokara-sample:0.1 zerokara.azurecr.io/zerokara-sample:0.1

Let's check the list of images once in this state.

Confirmation of Docker image

$ docker imagesREPOSITORY TAG IMAGE ID CREATEDSIZEzerokara-sample0.1 bb31f47d8cce2 months ago13.3kBzerokara.azurecr.io/zerokara-sample0.1 bb31f47d8cce2 months ago13.3kB

In addition to the Docker image built from DockerFile, the image of the other name in Docker Tag is now displayed in the list.Since both images are the same, you can see that "Image ID" and "size" are the same value.Finally, push the Docker image to the container registry with the "Docker Push" command.

Push the Docker image to the container registry

$ docker push zerokara.azurecr.io/zerokara-sample:0.1

After the push is completed, check the "repository" in the container registry menu from the Azure portal to see that the pusher image and tags are displayed.

If you push the Docker image to the ACR container registry, it will be listed in the "repository" menu.This time, only one tag of one Docker image was pushed, but you can register by registering multiple Docker images and multiple tags.

Pull the Docker image from ACR and start the container

Finally, let's try to start the container by pulling the Docker image registered in the ACR container registry on a local machine.Delete the Docker image used in the local machine to check the operation once.By using the "Docker RMI" command, you can delete the Docker image on the local machine.

Delete the Docker image once in the local machine

$ docker rmi zerokara.azurecr.io/zerokara-sample:0.1

If you execute the Docker Images command after deleting the Docker image, you can confirm that the corresponding Docker image has been deleted from the image list.Once you have confirmed that it has been deleted, pull the image from the ACR container registry.

Pull the Docker image from the ACR container registry

$ docker pull zerokara.azurecr.io/zerokara-sample:0.10.1: Pulling from zerokara-sampleDigest: sha256:f778eedb947aadf60ec077c8458c1abed9a05d98cd192d77b2cfbc560c5a37dcStatus: Downloaded newer image for zerokara.azurecr.io/zerokara-sample:0.1zerokara.azurecr.io/zerokara-sample:0.1

By executing the "Docker Pull" command, you can pull (download) a Docker image with the corresponding Docker image name and tag from the host of the container registry specified in the argument.If you execute the Docker Images command after the pull is completed, you can see that the corresponding Docker image is displayed again in the list of images.Let's start the container using the pulled docker image.

Start the container using the docker image pulled from the container registry

$ docker run zerokara.azurecr.io/zerokara-sample:0.1Hello from Docker!This message shows that your installation appears to be working correctly.・・・中略

It was confirmed that the container was launched by specifying the Docker image pulled from the ACR container registry in the argument of the Docker Run command, and the same execution result was displayed.

In addition, the Azure Container Registry will be charged during the container registry, so please delete the container registry created this time as needed.

Delete container registry

az acr delete --name zerokara

Please note that if you delete the container registry, the pushed container image will be deleted.

summary

This time, we introduced services for container -based applications provided by Azure and how to use Azure Container Registry (ACR).You've probably understood how to push or pull a Docker image to ACR using the Docker command.ACR is a service used from Azure services that run other container applications, so you need to understand how to use it.Next time, I will continue to take up Azure Container Registry and explain how to create a container image using a function called ACR task.

Related Articles