├── Dockerfile ├── LICENSE ├── README-Docker.md ├── README.md ├── deployment.yml └── service.yml /Dockerfile: -------------------------------------------------------------------------------- 1 | #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. 2 | 3 | FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base 4 | WORKDIR /app 5 | EXPOSE 80 6 | EXPOSE 443 7 | 8 | 9 | FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build 10 | WORKDIR /src 11 | COPY ["weatherapi.csproj", ""] 12 | RUN dotnet restore "./weatherapi.csproj" 13 | COPY . . 14 | WORKDIR "/src/." 15 | RUN dotnet build "weatherapi.csproj" -c Release -o /app/build 16 | 17 | FROM build AS publish 18 | RUN dotnet publish "weatherapi.csproj" -c Release -o /app/publish 19 | 20 | FROM base AS final 21 | WORKDIR /app 22 | COPY --from=publish /app/publish . 23 | ENTRYPOINT ["dotnet", "weatherapi.dll"] 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 AZURE PROJECTS 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README-Docker.md: -------------------------------------------------------------------------------- 1 | # Containerize .NET Core 3.1 Web API using Docker 2 | 3 | 4 | ## Prerequisites 5 | 1. Install docker desktop on windows machine 6 | 7 | 2. Install Azure CLI on windows machine 8 | 9 | 3. Create Service Principal in Azure AD 10 | 11 | 4. Create Azure Container Registry 12 | 13 | 5. Configure RBAC in Azure Container Registry (Acrpull) for the Service Principal 14 | 15 | 16 | ## Docker Commands 17 | 18 | ### Get docker version details 19 | docker version 20 | 21 | ### Get docker version number 22 | docker --version 23 | 24 | ### Get list of docker images 25 | docker images 26 | 27 | ### Remove a docker image 28 | docker rmi -f 46e18b10d5fe 29 | 30 | ### Build docker image 31 | docker build -t weatherapi:v1 . 32 | 33 | *NOTE: . denotes the current directory* 34 | 35 | ### Retag docker image to ACR (Azure Container Registry) 36 | docker tag weatherapi:v1 acrweather.azurecr.io/weatherapi:v1 37 | 38 | ### Login to Azure Portal 39 | az login 40 | 41 | *NOTE: This opens up the Azure AD login page, enter the credential & login should happen* 42 | 43 | ### Login to Azure Container Registry 44 | az acr login --name acrweather 45 | 46 | *NOTE: Login to the Azure Container Registry. This step is important, else you will get unauthorized error while pushing images to ACR* 47 | 48 | ### Push docker images to ACR 49 | docker push acrweather.azurecr.io/weatherapi:v1 50 | 51 | 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker & Kubernetes Notes 2 | Containirize dotnet core web Api using Docker, push to Azure Container Registry and deploy to Kubernetes cluster in AKS 3 | 4 | 5 | ## Pre-requisites 6 | 1. Install docker desktop on windows machine 7 | 8 | 2. Install Azure CLI on windows machine 9 | 10 | 3. Create Service Principal in Azure AD 11 | 12 | 4. Create Azure Container Registry 13 | 14 | 5. Configure RBAC in Azure Container Registry (Acrpull) for the Service Principal 15 | 16 | 17 | 18 | ## [Docker - Containerize dotnet core Web Api](https://github.com/nidhisht/DockerKubernetesNotes/blob/master/README-Docker.md) 19 | 20 | 21 | 22 | ## Service Principal 23 | 24 | 1. az login 25 | 26 | 2. az account show 27 | 28 | 3. az ad sp create-for-rbac --skip-assignment 29 | 30 | 31 | ## Azure Container Registry - Push Docker images 32 | 33 | 1. az acr login --name retailcontainerregistry 34 | 35 | 2. docker images 36 | 37 | 3. docker tag orderservice:v2 retailcontainerregistry.azurecr.io/orderservice:v2 38 | 39 | 4. docker images 40 | 41 | 5. docker push retailcontainerregistry.azurecr.io/orderservice:v2 42 | 43 | ## Azure Kubernetes Service - Create Kubernetes Cluster & run containers 44 | 45 | 1. az aks get-credentials --resource-group rg-cosmos --name retailkubernetescluster 46 | 47 | 2. kubectl get nodes 48 | 49 | 3. kubectl apply -f .\Deployment.yml 50 | 51 | 4. kubectl get deployment 52 | 53 | 5. kubectl get pods 54 | 55 | 6. kubectl apply -f .\Service.yml 56 | 57 | 7. kubectl get services 58 | 59 | ### [Reference](https://www.youtube.com/watch?v=vBx7WY25fM0) 60 | -------------------------------------------------------------------------------- /deployment.yml: -------------------------------------------------------------------------------- 1 | 2 | apiVersion: apps/v1 3 | kind: Deployment 4 | metadata: 5 | name: weatherapi-deployment 6 | spec: 7 | selector: 8 | matchLabels: 9 | app: weatherapi-pod 10 | template: 11 | metadata: 12 | labels: 13 | app: weatherapi-pod 14 | spec: 15 | containers: 16 | - name: weatherapi-container 17 | image: acrweather.azurecr.io/weatherapi:v1 18 | resources: 19 | limits: 20 | memory: "128Mi" 21 | cpu: "500m" 22 | ports: 23 | - containerPort: 80 24 | -------------------------------------------------------------------------------- /service.yml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: weatherapi-service 5 | spec: 6 | selector: 7 | app: weatherapi-pod 8 | ports: 9 | - port: 8080 10 | targetPort: 80 11 | type: LoadBalancer 12 | 13 | --------------------------------------------------------------------------------