Lab 05: Deploy compute workloads by using images and containers
Microsoft Azure user interface
Given the dynamic nature of Microsoft cloud tools, you might experience Azure UI changes that occur after this training content’s development. As a result, the lab instructions and lab steps might not align correctly.
Microsoft updates this training course when the community alerts us to needed changes. However, cloud updates occur frequently, so you might encounter UI changes before this training content updates. If this occurs, adapt to the changes, and then work through them in the labs as needed.
Instructions
Before you start
Sign in to the lab environment
Sign in to your Windows 10 virtual machine (VM) by using the following credentials:
-
Username: Admin
-
Password: Pa55w.rd
Note: Your instructor will provide instructions to connect to the virtual lab environment.
Review the installed applications
Find the taskbar on your Windows 10 desktop. The taskbar contains the icons for the applications that you’ll use in this lab, including:
-
Microsoft Edge
-
File Explorer
Architecture diagram
Exercise 1: Create a VM by using the Azure Command-Line Interface (CLI)
Task 1: Open the Azure portal
-
On the taskbar, select the Microsoft Edge icon.
-
In the open browser window, navigate to the Azure portal (https://portal.azure.com), and then sign in with the account you’ll be using for this lab.
Note: If this is your first time signing in to the Azure portal, you’ll be offered a tour of the portal. If you prefer to skip the tour, select Get Started to begin using the portal.
Task 2: Create a resource group
-
On the Azure portal’s navigation pane, use the Search resources, services, and docs text box to search for Resource group, and then in the list of results, select Resource groups.
-
On the Resource groups blade, select Create.
-
On the Create a resource group blade, on the Basics tab, perform the following actions, and then select Review + create:
Setting Action Subscription drop-down list Retain the default value. Resource group text box Enter ContainerCompute Region drop-down list Select (US) East US The following screenshot displays the configured settings on the Create a resource group blade.
-
On the Review + create tab, review the options that you selected during the previous steps.
-
Select Create to create the resource group by using your specified configuration.
Note: Wait for the creation task to complete before moving forward with this lab.
Task 3: Open Azure Cloud Shell
-
In the Azure portal, select the Cloud Shell icon to open a new PowerShell session. If Cloud Shell defaults to a PowerShell session, select PowerShell, and then in the drop-down menu, select Bash.
Note: The Cloud Shell icon is represented by a greater than sign (>) and underscore character (_).
Note: If this is the first time you’re starting Cloud Shell, when prompted to select either Bash or PowerShell, select Bash. When you’re presented with the You have no storage mounted message, select the subscription you’re using in this lab, and then select Create storage.
-
At the Cloud Shell command prompt in the portal, run the following command to get the version of the Azure CLI tool:
az --version
Task 4: Use the Azure CLI commands
-
Run the following command to get a list of subgroups and commands at the root level of the CLI:
az --help
-
Run the following command to get a list of subgroups and commands for Azure Virtual Machines:
az vm --help
-
Run the following command to get a list of arguments and examples for the Create Virtual Machine command:
az vm create --help
-
Run the following command to create a new virtual machine with the following settings, be sure to record the password you are asked to create below you will need it later in the lab to access your virtual machine:
-
Resource group: ContainerCompute
-
Name: quickvm
-
Image: Debian
-
Admin-Username: student
-
Admin-Password: <CreateYourPassword>
Note: Replace
<CreateYourPassword>
in the command below with your own password.az vm create --resource-group ContainerCompute --name quickvm --image Debian --admin-username student --admin-password <CreateYourPassword>
Note: Wait for the VM to be created. After the process completes, the command will return a JavaScript Object Notation (JSON) file containing details about the machine.
-
-
Run the following command to get a more detailed JSON file that contains various metadata about the newly created VM:
az vm show --resource-group ContainerCompute --name quickvm
-
Run the following command to list all the IP addresses associated with the VM:
az vm list-ip-addresses --resource-group ContainerCompute --name quickvm
-
Run the following command to filter the output to only return the first IP address value:
az vm list-ip-addresses --resource-group ContainerCompute --name quickvm --query '[].{ip:virtualMachine.network.publicIpAddresses[0].ipAddress}' --output tsv
-
Run the following command to store the results of the previous command in a new Bash shell variable named ipAddress:
ipAddress=$(az vm list-ip-addresses --resource-group ContainerCompute --name quickvm --query '[].{ip:virtualMachine.network.publicIpAddresses[0].ipAddress}' --output tsv)
-
Run the following command to render the value of the Bash shell variable ipAddress:
echo $ipAddress
-
Run the following command to connect to the VM that you created previously in this lab, by using the Secure Shell (SSH) tool and the IP address stored in the Bash shell variable ipAddress:
ssh student@$ipAddress
-
The SSH tool informs you that the authenticity of the host can’t be established and then asks if you want to continue connecting. Enter yes, and then select Enter to continue connecting to the VM.
-
The SSH tool then asks for a password. Enter the password you created earlier, and then select Enter to authenticate with the VM.
-
After connecting to the VM by using SSH, run the following command to get metadata describing the Linux VM:
uname -a
-
Use the exit command to end your SSH session:
exit
-
Close the Cloud Shell pane in the portal.
Review
In this exercise, you used Cloud Shell to create a VM as part of an automated script.
Exercise 2: Create a Docker container image and deploy it to Azure Container Registry
Task 1: Open the Cloud Shell and editor
-
On the Azure portal’s navigation pane, select the Cloud Shell icon to open a new shell instance.
Note: Wait for Cloud Shell to finish connecting to an instance before moving on with the lab.
-
At the Cloud Shell command prompt in the portal, run the following command to move from the root directory to the ~/clouddrive directory:
cd ~/clouddrive
-
Run the following command to create a new directory named ipcheck in the ~/clouddrive directory:
mkdir ipcheck
-
Run the following command to change the active directory from ~/clouddrive to ~/clouddrive/ipcheck:
cd ~/clouddrive/ipcheck
-
Run the following command to create a new .NET console application in the current directory:
dotnet new console --output . --name ipcheck
-
Run the following command to create a new file in the ~/clouddrive/ipcheck directory named Dockerfile:
touch Dockerfile
-
Run the following command to open the embedded graphical editor in the context of the current directory:
code .
Task 2: Create and test a .NET application
-
In the graphical editor, on the FILES pane, select the Program.cs file to open it in the editor.
-
Delete the entire contents of the Program.cs file.
-
Copy and paste the following code into the Program.cs file:
public class Program { public static void Main(string[] args) { // Check if network is available if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { System.Console.WriteLine("Current IP Addresses:"); // Get host entry for current hostname string hostname = System.Net.Dns.GetHostName(); System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(hostname); // Iterate over each IP address and render their values foreach(System.Net.IPAddress address in host.AddressList) { System.Console.WriteLine($"\t{address}"); } } else { System.Console.WriteLine("No Network Connection"); } } }
-
Save the Program.cs file by using the menu in the graphical editor or the Ctrl+S keyboard shortcut. Don’t close the graphical editor.
-
Back at the command prompt, run the following command to run the application:
dotnet run
-
Review the results of the run. At least one IP address should be listed for the Cloud Shell instance.
-
In the graphical editor, on the FILES pane of the editor, select the Dockerfile file to open it in the editor.
-
Copy and paste the following code into the Dockerfile file:
# Start using the .NET Core 3.1 SDK container image FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build # Change current working directory WORKDIR /app # Copy existing files from host machine COPY . ./ # Publish application to the "out" folder RUN dotnet publish --configuration Release --output out # Start container by running application DLL ENTRYPOINT ["dotnet", "out/ipcheck.dll"]
-
Save the Dockerfile file by using the menu in the graphical editor or by using the Ctrl+S keyboard shortcut.
-
Leave the Cloud Shell open for the next task.
Task 3: Create a Container Registry resource
-
At the Cloud Shell command prompt in the portal, run the following command to create a variable with a unique value for the Container Registry resource:
registryName=conregistry$RANDOM
-
At the Cloud Shell command prompt in the portal, run the following command to verify the name created in the previous step is available:
az acr check-name --name $registryName
If the results show the name is available, continue to the next step. If the name is not available then re-run the command in the previous step and verify availability again.
-
At the Cloud Shell command prompt in the portal, run the following command to create a Container Registry resource:
az acr create --resource-group ContainerCompute --name $registryName --sku Basic
Note: Wait for the creation task to complete before you continue with this lab.
Task 4: Store Container Registry metadata
-
At the Cloud Shell command prompt in the portal, run the following command to get a list of all container registries in your subscription:
az acr list
-
Run the following command, ensuring you see the name of your registry as output. If you see no output other than ‘[]’, wait a minute and try running the command again.
az acr list --query "max_by([], &creationDate).name" --output tsv
-
Run the following command:
acrName=$(az acr list --query "max_by([], &creationDate).name" --output tsv)
-
Run the following command:
echo $acrName
Task 5: Deploy a Docker container image to Container Registry
-
Run the following command to change the active directory from ~/ to ~/clouddrive/ipcheck:
cd ~/clouddrive/ipcheck
-
Run the following command to get the contents of the current directory:
dir
-
Run the following command to upload the source code to your container registry and build the container image as a Container Registry task:
az acr build --registry $acrName --image ipcheck:latest .
Note: Wait for the build task to complete before moving forward with this lab.
-
Close the Cloud Shell pane in the portal.
Task 6: Validate your container image in Container Registry
-
On the Azure portal’s navigation pane, select the Resource groups link.
-
From the Resource groups blade, select the ContainerCompute resource group that you created previously in this lab.
-
From the ContainerCompute blade, select the container registry that you created previously in this lab.
-
From the Container Registry blade, in the Services section, select the Repositories link.
-
In the Repositories section, select the ipcheck container image repository, and then select the latest tag.
-
Review the metadata for the version of your container image with the latest tag.
Note: You can also select the Run ID link to find metadata about the build task.
Review
In this exercise, you created a .NET console application to display a machine’s current IP address. You then added the Dockerfile file to the application so that it could be converted into a Docker container image. Finally, you deployed the container image to Container Registry.
Exercise 3: Deploy an Azure container instance
Task 1: Enable the admin user in Container Registry
-
On the Azure portal’s navigation pane, select the Resource groups link.
-
On the Resource groups blade, select the ContainerCompute resource group that you created previously in this lab.
-
On the ContainerCompute blade, select the container registry that you created previously in this lab, and then select Update.
-
On the Update container registry blade, in the Admin user section, select Enable.
-
Select Save, and then close the Update container registry blade.
Task 2: Automatically deploy a container image to an Azure container instance
-
On the Container Registry blade, in the Services section, select the Repositories link.
-
In the Repositories section, select the ipcheck container image repository.
-
On the Repository blade, select the ellipsis menu associated with the latest tag entry, and then select Run instance.
-
On the Create container instance blade, perform the following actions, and then select OK:
Setting Action Container name text box Enter managedcompute Container image text box Retain the default value. OS type section Select Linux Subscription text box Retain the default value. Resource group drop-down list Select ContainerCompute Location drop-down list Select East US Number of cores drop-down list Select 2 Memory (GB) text box Enter 4 Public IP address section Select No The following screenshot displays the configured settings on the Create container instance blade.
Note: Wait for the container instance to be created before you continue with this lab.
Task 3: Manually deploy a container image to Container Instances
-
On the Azure portal’s navigation pane, select the Create a resource link.
-
On the Create a resource blade, in the Search services and marketplace text box, enter container instances, and then select Enter.
-
On the Marketplace search results blade, select the Container Instances result.
-
On the Container Instances blade, select Create.
-
On the Create Container Instance blade, on the Basics tab, perform the following actions, and then select Review + create:
Setting Action Subscription drop-down list Retain the default value Resource group drop-down list Select ContainerCompute Container name text box Enter manualcompute Region drop-down list Select (US) East US Image source section Select Azure Container Registry Registry drop-down list Select the Azure Container Registry resource that you created previously in this lab Image drop-down list Select ipcheck Image tag drop-down list Select latest The following screenshot displays the configured settings on the Create container instance blade.
-
From the Review + create tab, review the selected options.
-
Select Create to create the container instance by using your specified configuration.
Note: Wait for the container instance to be created before you continue with this lab.
Task 4: Validate that the container instance ran successfully
-
On the Azure portal’s navigation pane, select the Resource groups link.
-
On the Resource groups blade, select the ContainerCompute resource group that you created previously in this lab.
-
On the ContainerCompute blade, select the manualcompute container instance that you created previously in this lab.
-
On the Container Instances blade, in the Settings section, select the Containers link.
-
In the Containers section, review the list of Events.
-
Select the Logs tab, and then review the text logs from the container instance.
Note: You can also optionally find the Events and Logs from the managedcompute container instance.
Note: After the application finishes running, the container terminates because it has completed its work. For the manually created container instance, you indicated that a successful exit was acceptable, so the container ran once. The automatically created instance didn’t offer this option, and it assumes the container should always be running, so you’ll notice repeated restarts of the container.
Review
In this exercise, you used multiple methods to deploy a container image to an Azure container instance. By using the manual method, you were able to customize the deployment further and to run task-based applications as part of a container run.
Exercise 4: Clean up your subscription
Task 1: Open Azure Cloud Shell and list resource groups
-
In the Azure portal, select the Cloud Shell icon to open a new Bash session. If Cloud Shell defaults to a PowerShell session, select PowerShell, and then in the drop-down menu, select Bash.
Note: If this is the first time you’re starting Cloud Shell, when prompted to select either Bash or PowerShell, select PowerShell. When you’re presented with the You have no storage mounted message, select the subscription you’re using in this lab, and then select Create storage.
Task 2: Delete resource groups
-
On the Cloud Shell pane, run the following command to delete the ContainerCompute resource group:
az group delete --name ContainerCompute --no-wait --yes
Note: The command executes asynchronously (as determined by the –no-wait parameter), so while you’ll be able to run another Azure CLI command immediately afterwards within the same Bash session, it’ll take a few minutes before the resource groups are actually removed.
- Close the Cloud Shell pane in the portal.
Task 3: Close the active applications
- Close the currently running Microsoft Edge application.
Review
In this exercise, you cleaned up your subscription by removing the resource groups used in this lab.