Docker revolutionized software development by providing a lightweight, portable, and consistent environment across different operating systems. With Docker, applications and their dependencies are encapsulated within containers, allowing them to run consistently on any machine, from development to production. Containers provide a high degree of isolation, enabling applications to be easily deployed and scaled without interference or conflicts. Docker simplifies the process of software deployment, making it efficient and reproducible, while also promoting collaboration and accelerating the development cycle. It has become a standard tool for building and deploying modern applications in various industries. In this blog, we will see how a .NET core web API can be deployed to docker installed locally.
Creating a .Net API
- Open the folder in which you want to create the web API
- Open Visual Studio Code
- Open the terminal inside the VS code
- Type
dotnet --helpto check the .NET Core version - Create a new web API. Run command
dotnet new webapi - The above code will generate a “weatherforecast” web API with all the required files
- The API will be generated with the folder name i.e. all the namespaces will be set to the folder name, so choose the folder name wisely.
- The .NET application can now be executed with the command
dotnet run - If there is an error related to SSL while building the project like “Unable to configure HTTPS endpoint”. Run the following command in terminal
dotnet dev-certs https --trust. Re-run the app again using the command in step 8 - Run the following in the browser to see the results
http://localhost:5154/weatherforecast
Deploy .Net Core web API to docker
Now once you are able to run the .NET core applications locally. Let’s run the same application in docker installed locally in WSL. This blog assumes, you have already installed the docker and Docker compose in WSL. If not “Click Here” for steps to install docker and Docker-Compose
To run a .NET Core application in Docker, you can follow these steps:
- Create a Dockerfile in the root directory(directory containing the .csproj file)
- Add the following content to the file
FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal AS base
WORKDIR /app
EXPOSE 5000
ENV ASPNETCORE_URLS=http://+:5000
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
WORKDIR /src
COPY ["dotnetcoredocker.csproj", "./"]
RUN dotnet restore "dotnetcoredocker.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "dotnetcoredocker.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "dotnetcoredocker.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "dotnetcoredocker.dll"]
Make sure to replace YourAppName.dll with the actual name of your .NET Core application’s entry point DLL file.
- Build the Docker image: Open a terminal or command prompt in the same directory as the
Dockerfile. Run the following command to build the Docker image:
docker build -t your-image-name .
Replace your-image-name with the desired name for your Docker image. The . at the end denotes the current directory.
- Create a docker-compose.yml in the root directory and add the following code to it
version: '3.4'
services:
dotnetcoredocker:
image: your-image-name
build:
context: .
dockerfile: ./Dockerfile
ports:
- 5000:5000
- Start container “docker compose up -d” in terminal.
That’s it! Your .NET Core application should now be running in a Docker container. You can access it by visiting “http://localhost:5000/weatherforecast” in a rest client or in your web browser, assuming your application is a web application listening on port 80. Adjust the URL accordingly if your application has a different endpoint.
you can find the sample project on github.
Leave a comment