Translate

Tuesday 27 June 2023

Create Docker images for .Net Core Microservice or Build and Deploy Microservices using Docker Compose file or .Net Core microservices with docker or Create new Microservice using .NET Core and build and run an image of it using Docker

        Microservices:
   Microservices is an approach to develop small services that each run in its own process. We should develop microservices instead of one service (a monolithic approach) for a multitude of benefits, including:

  • Microservices are smaller in size 
  • Microservices are easier to develop, deploy, and debug, because a fix only needs to be deployed onto the microservice with the bug, instead of across the board 
  • Microservices can be scaled quickly and can be reused among different projects 
  • Microservices work well with containers like Docker 
  • Microservices are independent of each other, meaning that if one of the microservices goes down, there is little risk of the full application shutting down 
Docker:

   Docker is a tool that makes it easier to create, deploy, and run applications by using a containerization approach. These containers are lightweight and take less time to start than traditional servers. These containers also increase performance and lower cost, while offering proper resource management. Another benefit to using Docker is that you no longer need to pre-allocate RAM to each container.

Create new Microservice using .NET Core and build and run an image of it using Docker: 

Step 1: 

Create a microservice (.NET Core WebAPI) with Docker support as shown below:
  • Select “ASP.NET Core Web Application (.NET Core)” from the drop-down menu.  
  • Select the “Enable Docker Support” option. 

 

 Clone the below project URL using git bash and do the following steps to create docker file and docker-compose file. 


 

A Docker file is a text file that contains instructions on how to build a Docker image and a  Docker compose file is a YAML file that groups together several docker containers for build and deployment.

 

git clone https://github.com/MicrosoftDocs/mslearn-dotnetmicroservices

 

Dockerfile

This file is the entry point for running any Docker application.
It is used to build an image of the application’s published code in “obj/Docker/publish.”


How to create Dockerfile:


FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build


WORKDIR /src


COPY backend.csproj .


RUN dotnet restore


COPY . .


RUN dotnet publish -c release -o /app


This code will perform the following steps sequentially when invoked:
  • Pull the mcr.microsoft.com/dotnet/sdk:6.0 image and name the image build 
  • Set the working directory within the image to /src 
  • Copy the file named backend.csproj found locally to the /src directory that you created 
  • Calls dotnet restore on the project 
  • Copy everything in the local working directory to the image 
  • Calls dotnet publish on the project 

                FROM mcr.microsoft.com/dotnet/aspnet:6.0

                WORKDIR /app

                EXPOSE 80

                EXPOSE 443

                COPY --from=build /app .

                ENTRYPOINT ["dotnet", "backend.dll"]

This code will perform the following steps sequentially when invoked:
  • Pull the mcr.microsoft.com/dotnet/aspnet:6.0 image 
  • Set the working directory within the image to /app 
  • Exposes port 80 and 443 
  • Copy everything from the /app directory of the build image you created into the app directory of this image 
  • Sets the entry point of this image to dotnet and passes backend.dll as an argument 

    docker-compose.override.yml : This file is used when running an application using Visual Studio.


Step 2:

    Open a command prompt, and go to the directory that holds that docker file.

                     docker build -t pizzabackend .  

Step 3:
        To run the web API service, run the following command to start a new Docker container using the pizzabackend image and expose the service on port 5200

                     docker run -it --rm -p 5200:80 --name pizzabackendcontainer pizzabackend 

 

Docker Commands: 

             To restore packages: dotnet restore

  • To publish the application code: dotnet publish -o obj/Docker/publish 
  •  To build the image: docker build -t coreapp  
  •  Run the image in a container: docker run -d -p 8001:80 –name core1 coreappimage 
  •  Check the running container: Docker PS 
  •  We can run the same image in multiple containers at the same time by using: 
                    docker run -d -p 8002:80 –name core2 coreappimage 
                    docker run -d -p 8003:83 –name core3 coreappimage 


Orchestrator:

        The orchestrator helps with composing applications consisting of many microservices into one deployable unit. That unit is then moved — or deployed — to a host.

 

Docker Compose
The Docker Compose tool then provides a way to build the individual services, and a means to start them.

Create a docker compose YAML file:

             1. Add the following code to the docker-compose.yml file:

     Yml:

version: '3.4'

services:
frontend:
image: pizzafrontend
build:
context: frontend
dockerfile: Dockerfile

environment:
- backendUrl=http://backend
ports:
- "5902:80"
depends_on:
- backend
backend:
image: pizzabackend
build:
context: backend
dockerfile: Dockerfile
ports:
- "5000:80" 


2. To build the container images, open a command prompt, navigate to the directory with the docker-compose.yml file, and run the following command:

  • docker-compose build

3. Then, to start both the website and the web API, run this command:

  • docker-compose up

No comments:

Post a Comment