Esercizio: Compilare e archiviare un'immagine di contenitore usando Attività del Registro Azure Container
In questo esercizio viene usato Attività del Registro Azure Container per eseguire le azioni seguenti:
- Creare un Registro Azure Container
- Compilare un'immagine da un Dockerfile ed eseguirne il push
- Verificare i risultati
- Eseguire l'immagine nel Registro Azure Container
Prerequisiti
- Un account Azure con una sottoscrizione attiva. Se non si dispone ancora di una sottoscrizione, è possibile iscriversi alla versione di valutazione gratuita su https://azure.com/free
Accedere ad Azure e avviare Cloud Shell
Accedere al portale di Azure e aprire Cloud Shell.
Dopo l'apertura della shell, selezionare l'ambiente Bash.
Creare un Registro Azure Container
Creare un gruppo di risorse per il Registro di sistema. Sostituire
<myLocation>
nel comando seguente con una posizione nelle vicinanze.az group create --name az204-acr-rg --location <myLocation>
Creare un registro contenitori di base. Il nome del registro deve essere univoco in Azure e contenere da 5 a 50 caratteri alfanumerici. Sostituire
<myContainerRegistry>
nel comando seguente con un valore univoco.az acr create --resource-group az204-acr-rg \ --name <myContainerRegistry> --sku Basic
Nota
Il comando crea un registro Basic, ovvero un'opzione ottimizzata in termini di costo per sviluppatori che iniziano a usare Registro Azure Container.
Compilare un'immagine da un Dockerfile ed eseguirne il push
A questo punto usare Registro Azure Container per compilare un'immagine ed eseguirne il push in base a un Dockerfile locale.
Creare o passare a una directory locale e quindi usare il comando seguente per creare il Dockerfile. Il Dockerfile contiene una singola riga che fa riferimento all'immagine
hello-world
ospitata in Registro Microsoft Container.echo FROM mcr.microsoft.com/hello-world > Dockerfile
Eseguire il comando
az acr build
, che crea l'immagine e, una volta creata correttamente l'immagine,esegue il push nel registro. Sostituire<myContainerRegistry>
con il nome usato in precedenza.az acr build --image sample/hello-world:v1 \ --registry <myContainerRegistry> \ --file Dockerfile .
Di seguito è riportato un esempio abbreviato dell'output del comando precedente che mostra le ultime righe con i risultati finali. È possibile notare che nel campo
repository
è elencata l'immaginesample/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
Verificare i risultati
Usare il comando
az acr repository list
per elencare i repository nel registro. Sostituire<myContainerRegistry>
con il nome usato in precedenza.az acr repository list --name <myContainerRegistry> --output table
Output:
Result ---------------- sample/hello-world
Usare il comando
az acr repository show-tags
per elencare i tag nel repository sample/hello-world. Sostituire<myContainerRegistry>
con il nome usato in precedenza.az acr repository show-tags --name <myContainerRegistry> \ --repository sample/hello-world --output table
Output:
Result -------- v1
Eseguire l'immagine nel Registro Azure Container
Eseguire l'immagine del contenitore
sample/hello-world:v1
dal registro contenitori usando il comandoaz acr run
. Nell'esempio seguente viene usato$Registry
per specificare il registro in cui eseguire il comando: Sostituire<myContainerRegistry>
con il nome usato in precedenza.az acr run --registry <myContainerRegistry> \ --cmd '$Registry/sample/hello-world:v1' /dev/null
Il parametro
cmd
di questo esempio esegue il contenitore nella configurazione predefinita, macmd
supporta altri parametridocker run
o anche altri comandidocker
.L'output di esempio seguente è abbreviato:
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
Pulire le risorse
Quando il gruppo di risorse, il registro contenitori e le immagini del contenitore in esso archiviate non sono più necessari, è possibile usare il comando az group delete
per rimuoverli.
az group delete --name az204-acr-rg --no-wait