Docker with .NET 6.0 Web API
Create Docker Images with .NET 6.0 Web API
Docker is a software platform that facilitates the rapid development, testing, and deployment of programs. Docker software bundles programs into uniform units known as containers, each of which contains the libraries, runtime, code, and system tools required for the program to function.
Through this let us learn to create an image with a C# Web API running on .NET 6.0 and containerize the image and launch the docker running application.
Steps
Download docker desktop from this link
Install Docker desktop in the host machine
Sign in with an account
Create a Web Api application and add required controllers and services for the Api.
Now right click on the main project and select add, in there select ‘Docker Support’
After adding docker support, a file called ‘Dockerfile’ will be created in the directory, open the file and there will be few configurations present as below
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["<parent_dir>/<project_name>.csproj", " <parent_dir> /"]
RUN dotnet restore " <parent_dir>/<project_name>.csproj"
COPY . .
WORKDIR "/src/ <project_name> "
RUN dotnet build " <project_name>.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish " <project_name>.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", " <project_name>.dll"]
Now after the Docker file is created, publish the code either by CLI or in the Visual studio.
Through CLI
Navigate to the project solution folder and run “dotnet publish”
In Visual Studio
Right click on the project and click on publish.
Create a publish profile if not present and publish the code to a folder called /publish in the working directory
Next step is to create a docker image based on the dockerfile created. Open the command prompt and navigate to the folder where solution(.sln) file is present.
Make sure that the docker file generated is present in that location, if not copy the dockerfile and paste in this location. Now let's build the docker image using the following command
docker build -t <image_name> -f Dockerfile .
-t : defines the name of the image that is being created which is followed by the desired name
Image_name : any image name can be provided in small cases.
-f : defines the location of the dockerfile to build a docker image. By default, docker will search for a file named Dockerfile in the directory that is running if the file is present in any other location or if there is change in the file name then this tag can be used.
. : Note the ‘.’ at the end of the command which is necessary to copy the files from that directory
Now an image will be created, and we will be able to find them in the Docker Desktop.
If we run the below command, we will be able to see the list of images present in the docker. Verify the docker image is present in the list.
Now we need to run the image created which creates a container and will be able to launch the application in the port number that we provide.
docker run -d -p 1001:80 --name <container_name> <image_name>:<image_tag>
If we check the Docker Desktop, we will be able to find the container created and we can launch the application from the Docker Desktop
If we click on the port number, we will be able to launch the application
Miscellaneous
If the swagger does not launch if we try to launch the localhost then check the Startup.cs or Program.cs if there is any environment variable used
If it is present, then add env variable in the dockerfile.
If the application is build on .NET framework then run the docker on Windows containers as .NET framework is platform dependent. If it is .NET Core then it can be rum on Linux containers itself
References
Comments
Post a Comment