Bizi takip edin:

Docker’a Giriş

Klasik olarak bir tanımla başlamamız gerekirse Docker, dünyada en çok kullanılan yazılım konteynerleştirme platformudur. Konteynerleştirme konteyner içine koyma anlamına gelir. Docker, Linux Kernel’e 2008 yılında eklenen Linux Containers (LXC) üzerine kurulu bir teknolojidir. LXC, Linux’da aynı işletim sistemi içerisinde birbirinden izole olmuş bir biçimde çalışan Container’lar (Linux tabanlı sistemler) sağlamaktadır.

Docker’ı kullanarak her ortama hızla uygulama dağıtıp uygulamaları ölçeklendirebilir ve kodunuzun çalışacağından emin olabilirsiniz.

.NET Core Uygulaması Örneği

Öncelikle Visual Studiomuzdan Create New Project diyerek yeni bir .Net Core Uygulaması oluşturuyoruz.

Bu aşamada uygulamamızın içeriği önemli değil, bu yüzden Index’de küçük bir değişiklik yapıp uygulamamızı kaydediyoruz

Uygulamamızın dosya dizinine giderek Dockerfile adında bir dosya oluşturuyoruz. Bu dosyanın aynen bu isimde olması ve uzantısının olmaması docker daemon için çok önemli. Bu dosyanın içerisinde  docker daemon, uygulamamızı container haline getirirken hangi adımları takip etmesi gerektiğini belirtiyoruz. Her uygulama diline ve uygulama değişkenine göre bu dosyanın özelleştirilmesi gerekir. Bizim yazacağımız dockerfile şuanda en basit haliyle aşağıdaki gibi olacak.

Bu adımdan sonra yapmamız gereken şey command line’dan Docker Desktop ile containerimizi derlemek. Bunun için bir komut satırı açıp dockerfile’ımızın bulunduğu dizine gidiyoruz. İlgili dizinde ise dockerfile’ımızı derlemek için aşağıdaki komutu çalıştırıyoruz.

Komutta . kullandığımız için ortamdaki dockerfile’ı dockertestv3 tagiyle birlikte derliyor ve image haline getiriyor. Ortaya çıkan image’in id’si ise aşağıdaki gibi 9b ile başlıyor. Bu image’i istersek 

docker run -it –rm -p 8080:80 dockertestv3

Komutu ile birlikte local docker instance’ımızında çalıtırabiliriz ancak bizim yapmak istediğimiz bunu app servicede PaaS olarak yönetilebilir bir şekilde yapmak olacak. Bu yüzden bu image’i bir repository’e göndermemiz gerekiyor. Bizim tercihimiz burada Azure Container registry olacak ancak siz isterseniz DockerHub’a veya başka bir repostirye yükleyebilirsiniz. App Service’imiz bizden çalıştıracağı uygulamayı istediği zaman daha sonrasında ACR’ın (Azure Container Registry) içindeki image’ı göstereceğiz.

Bunun için azure subscriptionumuzda 

Az acr create –name mspTestAcr –resourcegroup TestLab 

Komutu ile yeni bir container registry oluşturuyoruz. 

Tekrar komut satırımıza gelerek Az acr login –name mspTestAcr komutu ile login oluyoruz. Sonra sırasıyla aşağıdaki komutları uygulayarak image’ımızı registry’imze yolluyoruz.

Docker tag 9b6d3467d1e5 msptestacr.azurecr.io/dockerapp

Docker push msptestacr.azurecr.io/dockerapp

Latest tagi ile birlikte repostryimize image’ımız gelmiş bulunmakta. Bu adımdan sonra Web App’imizi oluşturabiliriz. Azure Portalden yeni bir Web App oluşturuyoruz.

Web App’imizi yaratırken sitenin koddan mı yoksa image’dan mı çalışacağını sorduğu bölümde Docker Container seçiyoruz.

Bir sonraki adımda ise uygulama image’ının nerede olduğunu ve hangi image olduğunu belirtiyoruz.

Bu şekilde servisimizi yarattığımız zaman artık image’ımızdan çalışan bir web appimiz olmuş oluyor.

Azure Devops Entegrasyonu ve CI/CD

Bu yaptığımız işlemlerin hepsini otomatize bir şekilde yapılmasını istersek eğerki burada karşımıza ücretsiz bir şekilde kullanabileceğimiz Azure DevOps servisi çıkıyor. Kısaca bahsetmem gerekirse Azure DevOps servisi bizim için Kanban Board – Work Items, Git Repostiry, CI/CD pipelines, Tests ve Artifacts gibi servisler sunarak uygulama hayat döngümüzü tamamen yönetilebilir bir hale getiriyor. Bizde burda uygulama kodlarımızı repostirymize push ediyoruz ve bu git’in master branchini dinleyen bir CI/CD pipeline kurguluyoruz.

Bizim  için halihazırda hazır bir template olan Docker build and publish an image to Azure Container Registry’i seçiyoruz ve aşağıdaki gibi yaml’ımız ortaya çıkıyor.

Bu işlemin sonucunda pipeline’ımızı çalıştırıyoruz ve 1 dakika içerisinde image’ımız derleniyor. Bir sonraki aşamada ise derlenen image’ın Container registry’e push edilmesini sağlayacağız. 

Pipeline’ımız başarılı bir şekilde çalıştı ve çıktı olarak bir image üretti. Azure Container registry’imize baktığımız zaman yeni image’imizin buraya geldiğini görebiliyoruz.

Bundan sonraki adım olarak ise yeni bir app service oluşturması adımını kurgulayacağız. Bu adımıda CD pipeline’ınında kurguluyoruz.

Artifact olarak Build pipeline’ımızı gösteriyoruz ve çıktı olarak ise App Service Deployment Template’ini seçiyoruz.

Ancak burada bize getirdiği taskı değil yeni bir task olarak Web App on Container task’ını seçiyoruz ve bilgileri aşağıdaki gibi giriyoruz.

Bu işlemin sonucunda pipeline’ımız otomatik olarak her kod pushlandığında yeni bir image alıyor ve ACR’a pushluyor. Pushlanan image azure web app’a deploy ediliyor. Sürecimiz tamamen otomatize olmuş oluyor.

Previous Post

What is Cognitive Services?

Next Post

Microsoft Azure App Service Slots

Whats is Docker?

If we need to start with a classic definition, Docker is the most used software container platform in the world. Containerization means putting it in a container. Docker is a technology built on Linux Containers (LXC) that was added to Linux Kernel in 2008. LXC provides Containers (Linux based systems) that work in a isolated form in Linux within the same operating system.

With Docker, you can quickly deploy applications to any environment, scale applications, and make sure your code will work any platform.

.NET Core App Sample

First of all, we create a new .Net Core Application by clicking Create New Project from our Visual Studio

At this stage, the content of our application is not important, so we make a small change in the Index and save our application.

We create a file named Dockerfile by move to the file directory of our application. It is very important for the docker daemon that this file is exactly the same and it has no extension on it. In this file, we specify the steps that docker daemon should follow while turning our application into a image. This file needs to be customized according to each application language and application variable. The dockerfile that we will write will be as follows in its simplest form.

What we need to do after this step is to compile our container with Docker Desktop from the command line. To do this, we open a command line and go to the directory where our dockerfile is located. In the related directory, we run the command below to compile our dockerfile.

In the command. Since we use it, it compiles the dockerfile in the environment with the dockertestv3 tag and turns it into an image. The resulting image’s id starts with 9b as below. If we want to use this image

docker run -it –rm -p 8080:80 dockertestv3

With the command, we can run it in our local docker instance, but what we want to do is to do this in a manageable way as PaaS in the app service in Azure. So we need to send this image to a repository. Our preference will be the Azure Container registry here, but you can upload it to DockerHub or any other repostiries if you wish. When our App Service requests the application it will run from us, we will show the image inside the ACR (Azure Container Registry) later.

For this, in our azure subscription

Az acr create –name mspTestAcr –resourcegroup TestLab 

We create a new container registry with the command.

Coming back to our command line, we login with the Az acr login –name mspTestAcr command. Then we send our image to our registry by following the commands below.

Docker tag 9b6d3467d1e5 msptestacr.azurecr.io/dockerapp

Docker push msptestacr.azurecr.io/dockerapp

With the latest tag, we have an image on our repostry. After this step, we can create our Web App. We are creating a new Web App from Azure Portal.

When creating our Web App, we choose Docker Container in the section where the site asks whether it will work from code or image.

In the next step, we indicate where the application image is and what image it is.

When we create our service in this way, we now have a working web app from our image.

Azure Devops Integration and CI/CD

If we want all of these processes to be done in an automated way, here comes the Azure DevOps service that we can use for free. Azure The DevOps service provides services such as Kanban Board – Work Items, Git Repostiry, CI / CD pipelines, Tests and Artifacts, making our application life cycle fully manageable. Here we push our application codes to our repostirym and set up a CI / CD pipeline that listens to the master branch of this git.

We choose Docker build and publish an image to Azure Container Registry, which is already a ready template for us, and our yaml emerges as below.

As a result of this process, we run our pipeline and our image is compiled in 1 minute. In the next step, we will ensure that the compiled image is pushed to the Container registry.

Our Pipeline worked successfully and produced an image as output. When we look at our Azure Container registry, we can see that our new image came here.

As the next step, we will create the step to create a new app service. In this step, we set up the CD pipeline.

We select our Build pipeline as Artifact and select the App Service Deployment Template as the output.

However, we choose the Web App on Container task as a new task, not the one it brings to us, and we enter the information as follows.

As a result of this process, our pipeline automatically gets a new image every time the code is pushed and pushes it into ACR. The pushed image is deployed to azure web app. Our process is fully automated.

Previous Post

What is Cognitive Services?

Next Post

Microsoft Azure App Service Slots

Start typing to see you are looking for.