Containerization
Containerization is a method of packaging software so it can run reliably across different computing environments. It wraps an application along with its dependencies, libraries, and configuration files into a single unit called a container. These containers are lightweight, portable, and isolated from the host system, making them ideal for consistent deployment across development, testing, and production.
Why use containerization?
- Portability: Run anywhere—local machine, cloud, or server.
- Isolation: Each container runs independently.
- Scalability: Easily replicate containers to handle load.
- Consistency: No more “works on my machine” issues.
What is Docker
Docker is a popular platform for developing, shipping, and running applications inside containers. It provides tools to create, manage, and orchestrate containers efficiently.
Example: Containerizing a Simple C# "Hello World" Console App
1. Create the C# App
bash
dotnet new console -o HelloWorldApp -n HelloWorld
This creates a folder HelloWorldApp with a Program.cs file that prints "Hello World".
2. Edit Program.cs (optional)
csharp
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello from a Docker container!");
}
}
}
3. Create a Dockerfile
Inside the HelloWorldApp folder, create a file named Dockerfile:
Dockerfile
# Use official .NET SDK image to build the app
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app
COPY . .
RUN dotnet publish -c Release -o out
# Use runtime image to run the app
FROM mcr.microsoft.com/dotnet/runtime:8.0
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "HelloWorld.dll"]
4. Build the Docker Image
bash
docker build -t helloworld-csharp .
5. Run the Container
bash
docker run helloworld-csharp
You should see:
Hello from a Docker container!