The Home controller needs to have an action that can handle the requests coming for a list of the latest articles. Here is the new controller with Articles action for GET requests:
The last thing we need to do is to add a View that will render our data. So, add an Articles View under Views/Home/ with the following code to render the latest articles,
Once you are done making the code changes, it's time to push your code on GitHub. Open a terminal and navigate to the project directory.
You can check the status of your local repository using the command,
You should see all the files and directories being added or update. Now, to stage all the changes in your repository run the command,
Let's commit the changes with a short meaningful message,
- $ git commit -m "your-commit-message"
Finally push all the committed changes to remote branch on GitHub,
Note
It is not a good practice to commit all the changes at the end. In fact, you should frequently commit all the changes that can be grouped logically.
At this point, I am assuming that your application is working well and you are ready containerize your application with Docker.
Dockerfile
According to Docker documentation,
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Docker can build images automatically by reading the instructions from a Dockerfile.
Here is the Dockerfile for my application,
-
- FROM microsoft/dotnet:2.1-sdk AS build-env
- WORKDIR /app
- COPY WebApp/*.csproj ./
- COPY . ./
- RUN dotnet restore
-
-
- FROM build-env AS publish
- RUN dotnet publish -c Release -o /app
-
-
- FROM microsoft/dotnet:2.1-aspnetcore-runtime
- WORKDIR /app
- LABEL Author="Gaurav Gahlot"
- LABEL Maintainer="quickdevnotes"
- COPY --from=publish /app .
- ENTRYPOINT ["dotnet", "WebApp.dll", "--server.urls", "http://*:80"]
Note that I'm using a multi-stage build to ensure that the final image is as small as possible.
At a high level, a multi-stage build is much like what we generally do while building and publishing our projects. Here is a brief breakdown of the stages,
- STAGE01 - At this stage we restore the packages and dependencies our application requires to build. Notice that the building the project .Net Core SDK, which is our base image.
- STAGE02 - Once the first stage is successful, we publish our application and store the generated binaries in /app directory.
- STAGE03 - In this stage we use the .Net Core Runtime as our base layer and copy the binaries from /appdirectory, generated in previous stage.
It's a great feature to minimize the size of your Docker images and you can read more about it from the docs.
Because this Dockerfile is specific to my application, you might have to make some changes for it to work.
Running a Container
It's time to build a Docker image for your application and spin up a container. Open up a terminal and navigate to the directory where the Dockerfile is saved. Now build a Docker image using the command:
- $ docker build -t webapp .
This will build a Docker image and keep it on our local system. To test our image and application we will now run a container using the command:
- $ docker run -d -p 5000:80 --rm --name webapp webapp
- 19c758fdb9bfb608c4b261c9f223d314fce91c6d71d33d972b79860c89dd9f15
The above command creates a container and prints the container ID as output. You may verify that the container is running using the docker ps command.
- $ docker ps
- CONTAINER ID IMAGE PORTS NAMES MOUNTS
- 19c758fdb9bf webapp 0.0.0.0:5000->80/tcp webapp
Now, open a browser and go to the URL http://localhost:5000/.
If everything is working fine, you must see your web application's home page. In my case it looks like this,
Test your application and once you are sure that it's working commit and push the Dockerfile to your GitHub repository.
Conclusion
In this we have developed a web application using ASP .Net Core and put it on a GitHub repository. We also tested our application by building a Docker image and running a Docker container out of it.
The next step is to setup a Build pipeline on Microsoft Azure DevOps. This pipeline will connect with our GitHub repository. We will also set the pipeline to trigger a build each time a push is made to a master branch on the repository. The pipeline will create a Docker image and push it to Docker Hub.