Dockerizing is the process of packing, deploying, and running applications using Docker containers. Docker is very popular among the developers. In this post, I am going to show you “Containerize Your .NET 7.0 Web Application With Docker”. for local development.
Before that, I will briefly explain what is docker and .NET
What Is Docker?
Docker is an open source tool that combines your application with all the necessary dependencies and libraries as one portable package (docker image). That package can be shared to any one and run by anyone without much worrying about the operating system.
What Is .NET?
.NET (pronounced as “dot net”; formerly named .NET Core) is a free and open-source, managed software framework for Windows, Linux, and macOS operating systems. It is a platform independent and successor to .NET Framework.
Dockerizing is the process of packing, deploying, and running applications using Docker containers.
Docker should be pre-installed in your machine. If you don’t have, please install form following link:
- Install docker form : https://docs.docker.com/engine/install/
Steps to Containerize .NET Web Application With Docker
.NET 7.0 Web Application can be decorize in following steps.
Step 1 : Create New .NET 7.0 Web Application
You have two options: you can either use an existing .NET 7.0 web application or create a new one.
For demonstration purposes, I will create a small .NET 7.0 web application that accesses the default API endpoint provided by the framework.
I will utilize Visual Studio to create the .NET 7.0 web application, assuming that you are already familiar with the process.
Upon creating the application, it will automatically generate a
WeatherForecastController.cs file by default. This file includes the default API to list the Weather Forecast. After containerizing the application, I will proceed to test the Weather Forecast API.
Here is the structure of my Dotnet application.
Step 2 : Create
Dockerfile inside root of your project.
This file should include following instruction.
Step 3 - Add a
docker-compose.yml in your project root. Please check in the attached project structure image. This
docker-compose.yml is not necessary, but it helps to build abd
run the docker image and container easily.
I have used following instruction in the
Port mapping is very important here step. In Stage 2 of
Dockerfile, where I have open a port on the Docker container. You might be thinking port 80 is default or common port and should be open by default, But in docker, it is not. By default, Docker doesn’t allow inbound requests to your container unless you explicitly tell Docker to open a port.
In this project I am exposing port
80 internally in side Docker and mapped to port
9000 of host machine form
docker-compose.yml file. So that I can run my application in
Step 4 - Build Docker Image
To build docker image, you have to run
docker compose build command. This command will take few minutes to
pull the dotnet image, install dependencies, copy files, and create app image. May be you can skip this step and directly
jump to step 5.
Step 5 - Start the containers
Now, you can run
docker compose up command. This command will start project in the container and
maps container’s internal port
80 to external port
docker compose up is successful, you can access your app locally via web browser
WeatherForecast is the default API provided by .NET 7.0.
To stop the containers, press Ctrl+C in the terminal.
Here is the output log for docker compose up
Step 6 - Test The Application
You should be able to access your application locally at port number
ou can browse
http://localhost:9000/WeatherForecast in your web browser, and you should be able to see below screen.
Finally, we are done. That’s it!
We have locally created .NET 7.0 Web Application, containerize the app using docker and ran with the help of docker-compose tool. Hope this steps helped you to skill up.