asp.net docker编译

DockerFile

# 使用官方 .NET SDK 作为基础镜像
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-env
WORKDIR /app

# 将项目文件复制到镜像中
COPY . ./


ARG build_channel=XX
RUN dotnet publish -c $build_channel -o /app

# 构建运行时镜像
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
COPY --from=build-env /app.

# 启动应用程序
ENTRYPOINT ["dotnet", "webapi.dll"]

如果包含aot需要加上这玩意

#RUN dotnet tool install -g dotnet-runtime
# 将项目文件复制到镜像中
COPY . ./
# AOT
#ENV PATH="${PATH}:/usr/local/bin"
RUN apt-get update && apt-get install -y gcc g++ zlib1g-dev\
    && export PATH="${PATH}:/usr/local/bin"

编译

docker build -t webapi --build-arg build_channel=MYCHANNEL .
asp.net docker编译_第1张图片
image.png

参考https://mcr.microsoft.com/en-us/product/dotnet/aspnet/abouthttps://github.com/dotnet/dotnet-docker/tree/main/samples/aspnetapp
提示
You intended to execute a .NET SDK command:
2023-08-18 13:35:04 No .NET SDKs were found.
可能是目录不对。

官方案例https://raw.githubusercontent.com/dotnet/dotnet-docker/main/samples/aspnetapp/Dockerfile

# Learn about building .NET container images:
# https://github.com/dotnet/dotnet-docker/blob/main/samples/README.md
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY aspnetapp/*.csproj .
RUN dotnet restore --use-current-runtime  

# copy everything else and build app
COPY aspnetapp/. .
RUN dotnet publish --use-current-runtime --self-contained false --no-restore -o /app


# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

你可能感兴趣的:(asp.net,docker,后端,容器,运维)