Skip to content

Latest commit

 

History

History
198 lines (110 loc) · 8.75 KB

100-mac.md

File metadata and controls

198 lines (110 loc) · 8.75 KB

Hands on Lab 100 - Using Docker Hub + Django Poll Application [ Using Mac Environment ]

The purpose of this Hands on Lab (HOL) is to have an understanding of how to:

  1. Install Docker Host and Docker CLI tools
  2. Build a custom Docker Image
  3. Push your custom Docker Image to a Docker Registry (this can be public/private Docker Hub or a private Azure Container Registry aka "ACR")
  4. Create an Azure App Service (Specifically a "Web App for Containers" Service)
  5. Deploy an instance of your custom Docker Image (container) to Azure App Service
  6. Create a webhook to update your Azure App Service when your custom Docker Image has been updated (new updates pushed to the Container Registry)

Pre-requisites:

  1. Azure Subscription or Sign up for Azure Fre Trial
  2. You will require access to a Mac Environment with Docker for this Hands on Lab (HOL)

Tasks

Create native Mac OS Environment

Before you install Docker on Mac OS

Requires Apple Mac OS Yosemite 10.10.3 or above. Download Docker Toolbox for previous OS versions.

Documentation : Docker on Mac

Setup Docker on Mac (Video)

Check out the video on how to install docker on Windows:https://www.youtube.com/watchv=lNkVxDSRo7M

For manual isntructions see below ....

Install

Double-click Docker.dmg to start the install process.

Get Docker CE for Mac

When the installation completes and Docker starts, the whale in the top status bar shows that Docker is running, and accessible from a terminal..

Run

Open a command-line terminal, and try out some Docker commands.

Run docker version to check that you have the latest release installed.

Run docker run hello-world to verify that Docker is pulling images and running as expected.

Pull the image files locally

Install GIT if not already installed on your machine

# Create a folder and go to that folder   
mkdir docker 
cd docker  

# Now Clone the repo locally  
git clone https://github.com/Azure-Samples/docker-django-webapp-linux.git  
cd docker-django-webapp-linux 

Locally build a Docker image

**Always run docker CLI commands on elevated privileges *

# You can edit the app by making changes to the html files under /app/templates/app folder. use vim [filepath] to open the file to edit. Here are commands if you are not familiar with vim  

# Now  build the image  [Usage : docker build -t [Image Name]:[tag]  Dockerfile Path]. Since you are inside  docker-django-webapp-linux folder you can use . to select the docker file in the current working directory 

docker build -t starterapp:latest . 

# Run docker images to see your image listed. Make a note of the IMAGE ID for your built image
docker images

# Sample output:
REPOSITORY                                         TAG                 IMAGE ID                         CREATED             SIZE 
starterapp                                         latest              <your_image_id>                  18 minutes ago      735MB

Login to Azure and Launch Azure Cloud Shell

Login via portal and launch cloud shell https://docs.microsoft.com/en-us/azure/cloud-shell/quickstart

1. Launch Azure Cloud Shell from the top navigation of the Azure portal
If it's the first time you open the Cloud Shell:
2. Select a subscription to create a storage account and Azure file share 
3. Select "Create storage" 

Tip

- You are automatically authenticated for Azure CLI 2.0 in every session.
- If you have multiple subscriptions, please use the following to choose the default subscription as the Azure Pass provided to you 

az account set --subscription my-subscription-name 

Note: You can also locally install Azure CLI on your local development environment as per these instructions

Create a resource group

# Use the Azure Cloud Shell console for the next commands.

# Use the[az appservice list-locations](https://docs.microsoft.com/en-us/cli/azure/appserviceview=azure-cli-latest#list-locations) Azure CLI command to list available locations. 
# Create a resource group for the ACR and web app 

az group create --name myResourceGroup --location "West US"    

Create Azure Container Registry

# Create a ACR container registry. Choose a unique name instead of myContainerRegistry

 az acr create --resource-group myResourceGroup --name myContainerRegistry --sku Basic
 
# Enable admin credentials on the new registry

az acr update -n myContainerRegistry --admin-enabled true
 
# Get Login credentials 

az acr credential show --name myContainerRegistry

Upload your image to the ACR registry

# Use your previously used local command line (PowerShell or CMD) for the next commands. 

# Login to ACR to pull or push images. Use the credentials received from the previous command. Ignore the security warning. Run docker CLI commands on administrator mode in Powershell on Command Prompt

docker login myContainerRegistry.azurecr.io -u <YOUR-USERNAME> -p <YOUR-PASSOWRD>    
        
# Tag the locally built image to ACR repo. Replace ```your_image_id``` with Image ID from locally building your image above. 

docker tag <your_image_id> myContainerRegistry.azurecr.io/starterapp:latest

# Push docker image to ACR . Replace ```<acrLoginServer>``` with the login server name of your ACR instance.

docker push myContainerRegistry.azurecr.io/starterapp:latest

# Verify the Push was successful. Do it in the Azure Cloud Shell

az acr repository list -n myContainerRegistry

Create an app service plan

#Executenext commands in the Azure Cloud Shell

az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku S1 --is-linux

Create a web app. Give it a unique name. Specify any runtime (it will be replaced later)

az webapp create --name <app_name> --resource-group myResourceGroup --plan myAppServicePlan --deployment-container-image-name <your-docker-user-name>/starterapp:latest

Configure web app to use ACR image

az webapp config container set command to assign the custom Docker image to the web app. Replace <app_name>, , , and . For Azure Container Registry, is in the format https://.azurecr.io.

   az webapp config container set --name <app_name> --resource-group myResourceGroup --docker-custom-image-name  myContainerRegistry.azurecr.io/starterapp --docker-registry-server-url https://myContainerRegistry.azurecr.io --docker-registry-server-user <registry-username> --docker-registry-server-password <password>

Restart your app

# Run this command 
 
az webapp restart --resource-group myResourceGroup --name <your_app_name>

Browse your app

https://<your_app_name>.azurewebsites.net 

Push an update to Docker image

Go back to Azure Virtual machine to make more changes. Build the image and then push it to your Docker Hub repository. Follow the steps above to do the same

Browse the app

http://<your_app_name>.azurewebsites.net

Configure CI/CD with ACR

Obtain a webhook

# You can obtain the Webhook URL 
 
az webapp deployment container show-cd-url -n <your_app_name> -g myResourceGroup

# For the Webhook URL, you need to have the following endpoint: 

https://<publishingusername>:<publishingpwd>@<your_app_name>.scm.azurewebsites.net/docker/hook

# You can obtain your publishingusername and publishingpwd by downloading the web app publish profile using the Azure portal.

Add a webhook to ACR

Replace <webhook-url-web app> with web hook URL endpoint https://<publishingusername>:<publishingpwd>@<your_app_name>.scm.azurewebsites.net/docker/hook

az acr webhook create --registry myContainerRegistry --name myacrwebhook01 --actions push --uri <webhook-url-web app>

When the image gets updated, the web app get updated automatically with the new image.

Push an update to Docker image

Go back to Azure Virtual machine to make more changes. Build the image and then push it to your Docker Hub repository. Follow the steps above to do the same

Browse the app

http://<your_app_name>.azurewebsites.net