Bagikan melalui


Tutorial: Create container images on a Linux Service Fabric cluster

This tutorial is part one of a tutorial series that demonstrates how to use containers in a Linux Service Fabric cluster. In this tutorial, a multi-container application is prepared for use with Service Fabric. In subsequent tutorials, these images are used as part of a Service Fabric application. Dalam tutorial ini, Anda akan mempelajari cara:

  • Clone application source from GitHub
  • Create a container image from the application source
  • Deploy an Azure Container Registry (ACR) instance
  • Menandai gambar kontainer untuk ACR
  • Mengunggah gambar ke ACR

In this tutorial series, you learn how to:

Prasyarat

  • Linux development environment set up for Service Fabric. Follow the instructions here to set up your Linux environment.
  • Tutorial ini mengharuskan Anda menjalankan Azure CLI versi 2.0.4 atau yang lebih baru. Jalankan az --version untuk menemukan versinya. Jika Anda perlu menginstal atau meningkatkan, lihat Menginstal Azure CLI.
  • Additionally, it requires that you have an Azure subscription available. For more information on a free trial version, go here.

Dapatkan kode aplikasi

The sample application used in this tutorial is a voting app. Aplikasi ini terdiri dari komponen web front-end dan instans Redis back-end. The components are packaged into container images.

Gunakan git untuk mengunduh salinan aplikasi ke lingkungan pengembangan Anda.

git clone https://github.com/Azure-Samples/service-fabric-containers.git

cd service-fabric-containers/Linux/container-tutorial/

The solution contains two folders and a 'docker-compose.yml' file. The 'azure-vote' folder contains the Python frontend service along with the Dockerfile used to build the image. The 'Voting' directory contains the Service Fabric application package that is deployed to the cluster. These directories contain the necessary assets for this tutorial.

Membuat gambar kontainer

Inside the azure-vote directory, run the following command to build the image for the front-end web component. This command uses the Dockerfile in this directory to build the image.

docker build -t azure-vote-front .

Nota

If you are getting permission denied then follow this documentation on how to work with docker without sudo.

This command can take some time since all the necessary dependencies need to be pulled from Docker Hub. When completed, use the docker images command to see the azure-vote-front image you just created.

docker images

Deploy Azure Container Registry

First run the az login command to sign in to your Azure account.

az login

Next, use the az account command to choose your subscription to create the Azure Container registry. You have to enter the subscription ID of your Azure subscription in place of <subscription_id>.

az account set --subscription <subscription_id>

Saat menyebarkan Azure Container Registry, Anda terlebih dahulu memerlukan grup sumber daya. Grup sumber daya Azure adalah kontainer logis tempat sumber daya Azure disebarkan dan dikelola.

Buat grup sumber daya dengan perintah az group create. In this example, a resource group named myResourceGroup is created in the westus region.

az group create --name <myResourceGroup> --location westus

Buat registri Azure Container dengan perintah az acr create . Replace <acrName> with the name of the container registry you want to create under your subscription. This name must be alphanumeric and unique.

az acr create --resource-group <myResourceGroup> --name <acrName> --sku Basic --admin-enabled true

Throughout the rest of this tutorial, we use "acrName" as a placeholder for the container registry name that you chose. Please make note of this value.

Sign in to your container registry

Sign in to your ACR instance before pushing images to it. Use the az acr login command to complete the operation. Provide the unique name given to the container registry when it was created.

az acr login --name <acrName>

Perintah mengembalikan pesan 'Login Berhasil' setelah selesai.

Menandai gambar kontainer

Setiap gambar kontainer perlu ditandai dengan nama loginServer registri. This tag is used for routing when pushing container images to an image registry.

Untuk melihat daftar gambar saat ini, gunakan perintah gambar docker .

docker images

Keluaran:

REPOSITORY                   TAG                 IMAGE ID            CREATED              SIZE
azure-vote-front             latest              052c549a75bf        About a minute ago   913MB

To get the loginServer name, run the following command:

az acr show --name <acrName> --query loginServer --output table

This outputs a table with the following results. This result will be used to tag your azure-vote-front image before pushing it to the container registry in the next step.

Result
------------------
<acrName>.azurecr.io

Now, tag the azure-vote-front image with the loginServer of your container registry. Selain itu, tambahkan :v1 ke akhir nama gambar. Tag ini menunjukkan versi gambar.

docker tag azure-vote-front <acrName>.azurecr.io/azure-vote-front:v1

Once tagged, run 'docker images' to verify the operation.

Keluaran:

REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
azure-vote-front                       latest              052c549a75bf        23 minutes ago      913MB
<acrName>.azurecr.io/azure-vote-front  v1                  052c549a75bf        23 minutes ago      913MB

Mendorong gambar ke registri

Push the azure-vote-front image to the registry.

Dengan menggunakan contoh berikut, ganti nama loginServer ACR dengan loginServer dari lingkungan Anda.

docker push <acrName>.azurecr.io/azure-vote-front:v1

The docker push commands take a couple of minutes to complete.

Mencamtumkan gambar dalam registri

To return a list of images that have been pushed to your Azure Container registry, use the az acr repository list command. Perbarui perintah dengan nama instans ACR.

az acr repository list --name <acrName> --output table

Keluaran:

Result
----------------
azure-vote-front

Pada penyelesaian tutorial, gambar kontainer telah disimpan dalam instans Azure Container Registry privat. This image is deployed from ACR to a Service Fabric cluster in subsequent tutorials.

Langkah berikutnya

In this tutorial, an application was pulled from GitHub and container images were created and pushed to a registry. Langkah-langkah berikut telah selesai:

  • Clone application source from GitHub
  • Create a container image from the application source
  • Deploy an Azure Container Registry (ACR) instance
  • Menandai gambar kontainer untuk ACR
  • Mengunggah gambar ke ACR

Advance to the next tutorial to learn about packaging containers into a Service Fabric application using Yeoman.