Recently, during an online webinar about Docker with students, some of the students asked us very basic queries and its real-time implementation. So we thought why not write simple blogs in laymen’s terms. And we explained “How to Dockerize in PHP?”.
For the uninitiated, we are an IT service firm called 9series. We are known as a DevOps service provider company that provides different kinds of services varying from IT consultancy services, making an application of iOS, Android, or build a website and containerization. We do all this. We often do webinars to helps those students who want to learn to code.
In this article, We will explain how to dockerize the .NET core application. Before we start the application, The most needed things are:
We want to dockerize the file so first need to create the docker file. And for that, these are the steps to need to be done…
1. Dockerfile is a text language that can build a docker image with a pack of the required dependencies based on the command written in the Dockerfile.
2. Create a Dockerfile in your project folder.
3. Add the text below to your Dockerfile for either Linux or Windows Containers. The tags below are multi-arch meaning they pull either Windows or Linux containers depending on what mode is set in Docker Desktop for Windows.
4. The Dockerfile assumes that your application is called asp.net app. Change the Dockerfile to use the DLL file of your project.
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
If I explain those steps then,
First of all, one needs to understand that we need to create a docker image, in order to dockerize the application, and in order to create a docker image we need to create dockerfile. So, create the dockerfile in your folder. Then do write the code given above and then remove the assumption of dockerfile.
To make your build context as small as possible add a .dockerignore file to your project folder and mention which file or folder not required to copy into the image during the build process.
Then we will have to build the Docker Image, and here are steps to do it,
1. Open a command prompt and navigate to your project folder.
2. Use the following commands to build and run your Docker image:
$ docker build -t aspnetapp.
$ docker run -d -p 8080:80 --name myapp aspnetapp
At last, all we need to do is view the page that is running from the container:
Go to localhost:8080 to access your app in a web browser.
So, That’s how you can build and dockerize .NET core applications. In the next article, we will explain the process of building, dockerize, and implementation of Python(Django). We are leading enterprise Docker consulting services provider company in USA.
So, stay tuned!