연습: Azure Container Registry 작업을 사용하여 컨테이너 이미지 빌드 및 실행

완료됨

이 연습에서는 ACR 작업을 사용하여 다음 작업을 수행합니다.

  • Azure Container Registry 만들기
  • Dockerfile에서 이미지 빌드 및 푸시
  • 결과 확인
  • ACR에서 이미지 실행

사전 요구 사항

  • 활성 구독이 있는 Azure 계정. 구독이 아직 없다면 https://azure.com/free에서 무료 평가판에 등록할 수 있습니다.

Azure에 로그인하고 Cloud Shell 시작

  1. Azure Portal에 로그인하고 Cloud Shell을 엽니다.

    The location of Cloud Shell launch button.

  2. 셸이 열리면 Bash 환경을 선택합니다.

    Selecting the Bash environment.

Azure Container Registry 만들기

  1. 레지스트리에 대한 리소스 그룹을 만들고 다음 명령에서 <myLocation>을 가까운 위치로 바꿉니다.

    az group create --name az204-acr-rg --location <myLocation>
    
  2. 기본 컨테이너 레지스트리를 만듭니다. 레지스트리 이름은 Azure 내에서 고유해야 하며, 5-50자의 영숫자만 포함해야 합니다. 다음 명령의 <myContainerRegistry>를 고유한 값으로 바꿉니다.

    az acr create --resource-group az204-acr-rg \
        --name <myContainerRegistry> --sku Basic
    

    참고

    명령은 개발자가 Azure Container Registry에 대해 학습하는 데 사용할 수 있는 비용 최적화 옵션인 기본 레지스트리를 만듭니다.

Dockerfile에서 이미지 빌드 및 푸시

이제 Azure Container Registry를 사용하여 로컬 Dockerfile을 기반으로 이미지를 빌드하고 푸시합니다.

  1. 로컬 디렉터리를 만들거나 로컬 디렉터리로 이동한 후, 다음 명령을 사용하여 Dockerfile을 만듭니다. Dockerfile은 Microsoft Container Registry에서 호스트되는 hello-world 이미지를 참조하는 한 줄을 포함합니다.

    echo FROM mcr.microsoft.com/hello-world > Dockerfile
    
  2. 이미지를 작성하는 az acr build 명령을 실행하고 이미지가 성공적으로 작성되면 이를 레지스트리에 푸시합니다. <myContainerRegistry>를 이전에 사용한 이름으로 바꿉니다.

    az acr build --image sample/hello-world:v1  \
        --registry <myContainerRegistry> \
        --file Dockerfile .
    

    다음은 최종 결과의 마지막 몇 줄을 보여 주는 이전 명령의 출력에 대한 축약된 샘플입니다. repository 필드에서 sample/hello-word 이미지가 나열된 것을 볼 수 있습니다.

    - image:
        registry: <myContainerRegistry>.azurecr.io
        repository: sample/hello-world
        tag: v1
        digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
      runtime-dependency:
        registry: mcr.microsoft.com
        repository: hello-world
        tag: latest
        digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
      git: {}
    
    
    Run ID: cf1 was successful after 11s
    

결과 확인

  1. az acr repository list 명령을 사용하여 레지스트리의 리포지토리를 나열합니다. <myContainerRegistry>를 이전에 사용한 이름으로 바꿉니다.

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

    출력:

    Result
    ----------------
    sample/hello-world
    
  2. az acr repository show-tags 명령을 사용하여 sample/hello-world 리포지토리의 태그를 나열합니다. <myContainerRegistry>를 이전에 사용한 이름으로 바꿉니다.

    az acr repository show-tags --name <myContainerRegistry> \
        --repository sample/hello-world --output table
    

    출력:

    Result
    --------
    v1
    

ACR에서 이미지 실행

  1. az acr run 명령을 사용하여 컨테이너 레지스트리에서 sample/hello-world:v1 컨테이너 이미지를 실행합니다. 다음 예에서는 $Registry를 사용하여 명령을 실행하는 레지스트리를 지정합니다. <myContainerRegistry>를 이전에 사용한 이름으로 바꿉니다.

    az acr run --registry <myContainerRegistry> \
        --cmd '$Registry/sample/hello-world:v1' /dev/null
    

    이 예제의 cmd 매개 변수는 컨테이너를 기본 구성으로 실행하지만, cmd는 추가 docker run 매개 변수 또는 다른 docker 명령도 지원합니다.

    다음은 출력의 축약된 샘플입니다.

    Packing source code into tar to upload...
    Uploading archived source code from '/tmp/run_archive_ebf74da7fcb04683867b129e2ccad5e1.tar.gz'...
    Sending context (1.855 KiB) to registry: mycontainerre...
    Queued a run with ID: cab
    Waiting for an agent...
    2019/03/19 19:01:53 Using acb_vol_60e9a538-b466-475f-9565-80c5b93eaa15 as the home volume
    2019/03/19 19:01:53 Creating Docker network: acb_default_network, driver: 'bridge'
    2019/03/19 19:01:53 Successfully set up Docker network: acb_default_network
    2019/03/19 19:01:53 Setting up Docker configuration...
    2019/03/19 19:01:54 Successfully set up Docker configuration
    2019/03/19 19:01:54 Logging in to registry: mycontainerregistry008.azurecr.io
    2019/03/19 19:01:55 Successfully logged into mycontainerregistry008.azurecr.io
    2019/03/19 19:01:55 Executing step ID: acb_step_0. Working directory: '', Network: 'acb_default_network'
    2019/03/19 19:01:55 Launching container with name: acb_step_0
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    
    2019/03/19 19:01:56 Successfully executed container: acb_step_0
    2019/03/19 19:01:56 Step ID: acb_step_0 marked as successful (elapsed time in seconds: 0.843801)
    
    Run ID: cab was successful after 6s
    

리소스 정리

더 이상 필요 없는 경우 az group delete 명령을 사용하여 리소스 그룹, 컨테이너 레지스트리 및 그 안에 저장된 컨테이너 이미지를 제거할 수 있습니다.

az group delete --name az204-acr-rg --no-wait