Popular blog tags

Part 2 - how to containerize a .NET Web application with Docker - using raw Ubuntu image

Published

his method uses a standard ubuntu image.

In the first stage, we manually install the .NET 10.0.300 SDK to compile the app.

In the second stage, we install only the ASP.NET Core 10.0 Runtime in the final production stage

 

Install native dependencies required by .NET (SSL and Internationalization)

Install the specific .NET 10.0.300 SDK manually

Install  the ASP.NET Core 10.0 Runtime manually

 


Create a Dockerfile in your project root:

# ==========================================
# Stage 1: Build Environment (Using Raw Ubuntu)
# ==========================================
FROM ubuntu:24.04 AS build-env
ENV DEBIAN_FRONTEND=noninteractive

# Install prerequisites to run the installation script
RUN apt-get update && apt-get install -y \
    curl \
    libicu-dev \
    && rm -rf /var/lib/apt/lists/*

# Manually download and install the specific .NET 10.0.300 SDK
RUN curl -sSL https://dot.net | bash /dev/stdin --version 10.0.300 --install-dir /usr/share/dotnet

# Configure environment variables so the system can find the 'dotnet' CLI
ENV PATH="${PATH}:/usr/share/dotnet"
ENV DOTNET_ROOT=/usr/share/dotnet

WORKDIR /src
COPY . .

# Build a framework-dependent release (NO --self-contained flag)
# This keeps the output folder incredibly small since it excludes runtime DLLs
RUN dotnet publish -c Release -o /app/publish

# ==========================================
# Stage 2: Final Production Image (Using Raw Ubuntu)
# ==========================================
FROM ubuntu:24.04
ENV DEBIAN_FRONTEND=noninteractive
WORKDIR /app

# Install native dependencies required by .NET (SSL and Internationalization)
RUN apt-get update && apt-get install -y \
    curl \
    libssl3 \
    libicu-dev \
    && rm -rf /var/lib/apt/lists/*

# Install ONLY the ASP.NET Core 10.0 Runtime (matching your SDK version)
# Using '--runtime aspnetcore' ensures the web server (Kestrel) files are included
RUN curl -sSL https://dot.net | bash /dev/stdin --channel 10.0 --runtime aspnetcore --install-dir /usr/share/dotnet

# Re-expose the dotnet path for the runtime container
ENV PATH="${PATH}:/usr/share/dotnet"
ENV DOTNET_ROOT=/usr/share/dotnet

# Copy the framework-dependent application binaries
COPY --from=build-env /app/publish .

# Configure ASP.NET Core to listen on port 8080 inside the container
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080

# Because it is framework-dependent, you MUST invoke it via the 'dotnet' command
ENTRYPOINT ["dotnet", "YourWebAppProjectName.dll"]