Popular blog tags

Part 1 - how to containerize a .NET application with Docker - using Alpine Linux image

Published

If you want the smallest possible footprint without using Microsoft images, you can use Alpine Linux. Because Alpine uses a different C library (musl), you must explicitly target linux-musl-x64 during compilation.

# Stage 1: Build on raw Alpine
FROM alpine:latest AS build-env

# Install bash, curl, and build-dependencies required for the installer script
RUN apk add --no-cache bash curl icu-libs libintl krb5-libs libgcc libstdc++

# Download and install .NET 10.0.300 SDK
RUN curl -sSL https://dot.net | bash /dev/stdin --version 10.0.300 --install-dir /usr/share/dotnet
ENV PATH="${PATH}:/usr/share/dotnet"

WORKDIR /src
COPY . .

# Crucial: Target linux-musl-x64 for Alpine compatibility
RUN dotnet publish -c Release -r linux-musl-x64 --self-contained true -o /app/publish

# Stage 2: Final minimal Alpine runtime
FROM alpine:latest
WORKDIR /app

# Install minimal OS-level dependencies for running native .NET code
RUN apk add --no-cache icu-libs libssl3 libstdc++

COPY --from=build-env /app/publish .

ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080

ENTRYPOINT ["./YourWebAppProjectName"]

 

需要安装 .NET 程序在 Linux 系统上运行所必需的底层系统依赖库(如 glibc、libssl、zlib、crypto 等)。