Docker is an open-source containerization platform that allows developers to quickly build, run, and deploy applications. It packages applications with all their libraries, configurations, and dependencies.
Docker primarily aims to automate the deployment of applications within containers, which can boot up in seconds.
Why Docker?
In the tech world, you’ve probably heard the phrase, “It works on my machine.” This usually happens due to different libraries, configurations, and dependencies required for the application to run on various operating systems. Managing these dependencies and configurations is a crucial task for the DevOps team. Docker has the capabilities to address these challenges in the software development lifecycle.
Docker assists in building and deploying distributed microservice applications through continuous integration and continuous deployment pipelines, saving a lot of time. It uses containers as units of software that package application code with all its dependencies, allowing the application to run quickly in isolated environments.
Benefits of Docker
Application Portability: Docker is a container platform that allows containers to run on physical machines, virtual machines, or any cloud provider with minimal modifications and in less time.
Faster Delivery and Deployment: Docker enables efficient building and deployment of application images at every step of the deployment phase.
Scalability: Docker offers scalability by easily increasing or decreasing the number of application instances in different environments.
Isolation: Docker runs applications in isolated environments with all their dependencies and configurations containerized.
Security: Docker ensures that applications running in different containers are isolated from each other and provides various security layers and tools to manage this.
High Performance: Docker generally operates faster and consumes fewer resources than virtual machines.
Version Control Management: Docker provides versioning capabilities to track container versions and allows rollbacks when necessary.
Containerization of Applications
Before we start with the containerization of the dotnet application, make sure that Docker is running but executing the following command.
sudo systemctl status docker
Create a docker image inside a dotnet web api application.
# Use the official .NET Core SDK as a parent image
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app
# Copy the project file and restore any dependencies (use .csproj for the project name)
COPY *.csproj ./
RUN dotnet restore
# Copy the rest of the application code
COPY . .
# Publish the application
RUN dotnet publish -c Release -o out
# Build the runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
COPY --from=build /app/out ./
# Expose the port your application will run on
EXPOSE 80
# Start the application
ENTRYPOINT ["dotnet", "Demo-Web-API.dll"]
Explanation
# Use the official .NET Core SDK as a parent image
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app
Begin by specifying the base image for your Docker container, which is mcr.microsoft.com/dotnet/sdk:8.0. This image utilises the official .NET Core SDK 8.0 as its foundation. To refer to this stage later, assign it the alias “build”.
Next, set the working directory inside the container to /app. This directory is where your application files will be copied and built.
# Copy the project file and restore any dependencies (use .csproj for the project name)
COPY *.csproj ./
RUN dotnet restore
To copy the project files (*.csproj) from your computer to the /app directory in the container, use the command COPY *.csproj ./. This step is separated to optimise Docker’s layer caching, ensuring that subsequent steps are executed when changes are made to your project file.
To restore the project’s dependencies and ensure all required packages are available for building the application, use the command RUN dotnet restore. This command will handle restoring all dependencies.
# Copy the rest of the application code
COPY . .
Next, copy the remaining source code and files from the local directory to the /app directory and build the application.
# Publish the application
RUN dotnet publish -c Release -o out
Use the command RUN dotnet publish -c Release -o out to restore project dependencies and required packages.
This command compiles the .NET Core Web API application in Release mode and publishes the output to the out directory. The -o out flag specifies the output directory.
# Build the runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
COPY –from=build /app/out ./
FROM mcr.microsoft.com/dotnet/runtime:7.0: This section initiates a new phase using the runtime image. This image is lightweight and includes only the runtime necessary to run .NET Core applications, as opposed to the SDK image, which is used for building.
WORKDIR /app: Sets the working directory to /app in this new phase.
COPY –from=build /app/out .: Copies the compiled application from the build stage to the current runtime stage. This ensures that only the essential components are included in the final runtime image.
# Expose the port your application will run on
EXPOSE 80
EXPOSE 80: This line specifies that the container will expose port 80.
# Start the application
ENTRYPOINT [“dotnet”, “Demo-Web-API.dll”]
Specifies the command to be executed when an object based on this image is started. In this case, it creates your .NET Core Web API Application by calling the dotnet Demo-Web-API.dll.
Build the docker image.
docker build -t web-api-demo .
The `docker build` command is used to create a Docker image from a Dockerfile. It offers various options, including the `-t` option to assign a tag to the image.
This command builds a Docker image using the Dockerfile located in the current directory (.) and tags it as `web-api-demo`.
Run the docker image inside a docker container.
docker run -d -p 5001:80 — name web-api-container web-api-demo
-d: Detached mode (runs in the background).
-p 5001:80: Maps port 5001 on your local machine to port 80 inside the container.
–name web-api-container: Assigns a name to the container.
web-api-demo: Uses the image you built previously.
Conclusion
In this article, we discussed the basics of docker and step-by-step implementation of the .NET Core 8 Web API application and containerization with the help of docker CLI.