다음을 통해 공유


Azure: Custom Docker container in Container Registry and deploy Web Apps for Containers

Introduction

On this article I´ll show you how to effective operationalize containers technology to build your apps faster and easier on Azure. This was first published in https://wikiazure.com/devops/build-run-and-deploy-docker-containers-to-azure/

This article offers some practical experience with Docker on Azure, with the goal of building and deploying Docker containers to Azure Container registry and deploy to an azure webapp.

Key Take-aways

  • A technique for moving your on-premises Docker containers to Azure
  • Deploy your container from Azure container registry to a web app (webapp for containers)

Overview

These are the overall steps that will follow along this article:

  • Build and run a container on your local machine
  • Prepare your Azure container Registry
  • Deploy your Docker container to Azure Container Registry
  • Provision your Docker container to Azure web App

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/00-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure-1024x288.png

Steps

  • Preparing your environment
  • Pull images from docker hub
  • Prepare Azure container Registry
  • Build your Docker container image
  • Push your Docker container image to Azure Container Registry
  • Deploy your container to azure webapp (webapp for containers)

Preparing your environment

Install docker desktop and choose your preferred OS

Docker Desktop for Windows download from: https://hub.docker.com/editions/community/docker-ce-desktop-windows

Sign in and download:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/01-Build-Run-and-Deploy-Docker-Container-to-Azure.pnghttps://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/02-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

The download process might take couple of minutes.

Install and setup Docker on Desktop

 Execute the downloaded exe file,  once the installation process starts, select your preferred configuration, in this case I´ll use the default setup.

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/03-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/04-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.pnghttps://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/05-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Once completed, you might want to restart and ensure you have enabled the hyper-v role on your local machine. 

Verify docker is running

Once we restart the local machine, we should see the docker icon in the taskbar

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/06-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Depending on your desktop setup you could probably see a notification pop up.

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/07-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Verify Docker version using PowerShell

Now we can use PowerShell or Visual Studio Code with the PowerShell extension, and execute: docker-version

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/08-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Check all images running on our machine, execute: docker images

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/09-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Now, where to get more container images from?

We have 2 options:

  1. Create your own images
  2. Pull them down from the repository online

Pull images from Docker Hub

Go to hub.docker.com

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/010-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

 Search for an image you want to pull, in this case I´ll look for wordpress image:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/011-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure-1024x288.png

 Pull the image to the local machine. Execute: docker pull wordpress

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/012-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

This might take a few minutes, Docker is got to reach out into that registry and pull on all you need to run that image on your local machine.

Once the download is completed, let´s verify the docker images.

Verify Docker images

Now we should see the wordpress image that we just pulled down from the Docker Hub.

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/013-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Now, you should be able to take that image and create running containers on you local machine.

Prepare Azure Container Registry

Go to your Azure subscription and create a new resource group, in this case I created a Resource group called: azure-advent-calendar

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/014-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Now we will create the azure container registry, which is the “place” where we are going to build and store our custom container image. So yes you might want to compare container registry = docker hub.

Go to the Azure Portal, click create new resource:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/015-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Look for the azure container registry service:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/016-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Then select the azure container registry:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/017-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Now click create Container Registry:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/018-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Provide the parameters for the creation of the Azure Container Registry:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/019-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Once created, go to the Azure Container Registry(ACR) blade and you should see all the properties related to the ACR:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/020-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure-1024x419.png

Copy the login server name, in this case: azureadventcalendar.azurecr.io

Now click on Access Keys, then enable user admins and copy the credentials:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/021-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Now open PowerShell and login to your ACR: docker login azureadventcalendar.azurecr.io

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/022-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Build Docker Container image

Create a directory for your local container image, in this case:

Mkdir ~/wordpress/

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/023-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Then let´s set the directory:

cd ~/wordpresss/

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/024-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Now create a new docker-compose.yml file in the new directory using visual studio and paste the code below:

version: '3.3'
 
services:
 
db:
 
image: mysql:5.7
 
volumes:
 
- db_data:/var/lib/mysql
 
restart: always
 
environment:
 
MYSQL_ROOT_PASSWORD: azureadventcalendar
 
MYSQL_DATABASE: wordpress
 
MYSQL_USER: wordpress
 
MYSQL_PASSWORD: wordpress
 
wordpress:
 
depends_on:
 
- db
 
image: wordpress:latest
 
ports:
 
- "8000:80"
 
restart: always
 
environment:
 
WORDPRESS_DB_HOST: db:3306
 
WORDPRESS_DB_USER: wordpress
 
WORDPRESS_DB_PASSWORD: wordpress
 
WORDPRESS_DB_NAME: wordpress
 
volumes:
 
db_data: {}

Now go back to PowerShell and execute the command to create the containers:

docker-compose up -d

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/001-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure-docker-compose.png

Now let´s verify that the containers are up and running, open in your browser: localhost:8000

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/025-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure-1024x494.png

Push your Docker container image to Azure

Login to your Azure Container Registry:

docker login azureadventcalendar.azurecr.io

Before pushing your docker image to the Azure Container Registry is important to apply a tag to your Docker container image. To add a tag to your Docker container image you can follow the syntax below:

docker tag <your image id> <you container registry loginservername>/wordpress

In this case:

docker tag f301ac1d3e93 azureadventcalendar.azurecr.io/wordpress

*Note: you can run “docker images” to get the image id that you should include when adding the tag

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/026-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Verify the images again and you should see the change reflected:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/027-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure-1024x78.png

Push to the Docker container image Azure Container Registry

Now let´s push to the Docker container image Azure Container Registry:

docker push azureadventcalendar.azurecr.io/wordpress

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/028-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure-1024x387.png

Optionally, you can perform the same process for the MySQL container:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/029-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Now go back to the Azure Portal to the Azure Container Registry, select the Repositories blade and you should see the wordpress image available:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/030-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Now select the image and the “latest” tag and you will be able to see all details as below:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/031-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure-1024x440.png

Deploy your Docker container from Azure Container Registry to a web app – “Web App for Containers"

From the Repositories >Tags, click on the options and click on deploy to web app as shown below:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/032-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Now provide the parameters to deploy the web app for containers:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/033-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure.png

Once the resource is provisioned you could take a look at the container settings within the web app:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/034-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure-1024x447.png

Now let´s finish the deployment by browsing to the URL:

az-adventcalendar.azurewebsites.net

After a few mins you should see the wordpress setup:

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/035-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure-1024x592.png

Now you have successfully deployed your wordpress Docker container to Azure web apps for containers!

https://wikiazurerep.azureedge.net/wp-content/uploads/2019/11/036-Build-Run-and-Deploy-Docker-Container-to-Azure-wikiazure-1024x546.png

Conclusion

Along this article we reviewed how to effectively make use of container technology using Docker and Azure Container Registry service. 

We also reviewed how to start from scratch with Docker, the installation process, then we moved on to the build process of a custom container image and how to leverage Azure Container Registry service to deploy your containerized application on top of the Azure web apps for containers service.