我需要知道如何重构Dockerfile与Dotnet应用程序



我需要重构Dotnet应用程序的Dockerfile,如下所示。

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80   
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["DockerExplained/DockerExplained.csproj", "DockerExplained/"]
RUN dotnet restore "DockerExplained/DockerExplained.csproj"
COPY . .
WORKDIR "/src/DockerExplained"
RUN dotnet build "DockerExplained.csproj" -c Release -o /app/build  
FROM build AS publish
RUN dotnet publish "DockerExplained.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerExplained.dll"]

因为我需要使用ubuntu20.04作为基础映像和图像层,我被告知不要提及busterslim。谁能指导一下如何进行这个?

========================================

这是对文件的新要求…要求是安装Ubuntu 20.04的原始映像中的所有内容,我无法打包从Ubuntu 20.04开始的映像层我得到了七个不同的图像,但我需要所有的东西在ubuntu 20.04中运行。

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["DockerExplained/DockerExplained.csproj", "DockerExplained/"]
RUN dotnet restore "DockerExplained/DockerExplained.csproj"
COPY . .
WORKDIR "/src/DockerExplained"
RUN dotnet publish "DockerExplained.csproj" -c Release -o /app/publish
FROM ubuntu:20.04
#Runtime-deps
FROM debian:buster-slim
RUN apt-get update 
&& apt-get install -y --no-install-recommends 
ca-certificates 

# .NET Core dependencies
libc6 
libgcc1 
libgssapi-krb5-2 
libicu63 
libssl1.1 
libstdc++6 
zlib1g 
&& rm -rf /var/lib/apt/lists/*
# Configure web servers to bind to port 80 when present
ENV ASPNETCORE_URLS=http://+:80 
# Enable detection of running in a container
DOTNET_RUNNING_IN_CONTAINER=true

#Runtime
from mcr.microsoft.com/dotnet/core/runtime-deps:3.1-buster-slim
RUN apt-get update 
&& apt-get install -y --no-install-recommends 
curl 
&& rm -rf /var/lib/apt/lists/*
# Install .NET Core
RUN dotnet_version=3.1.3 
&& curl -SL --output dotnet.tar.gz https://dotnetcli.azureedge.net/dotnet/Runtime/$dotnet_version/dotnet-runtime-$dotnet_version-linux-x64.tar.gz 
&& dotnet_sha512='e3f6f9b81bc3828b60f7da5a5c341373dc17f971f1de3f2714adcca180a630a60d4b681166fe78434d8b2ce023d2d08eff4f1935ec664130b7f856fa8e1cac2b' 
&& echo "$dotnet_sha512 dotnet.tar.gz" | sha512sum -c - 
&& mkdir -p /usr/share/dotnet 
&& tar -ozxf dotnet.tar.gz -C /usr/share/dotnet 
&& rm dotnet.tar.gz 
&& ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet
#ASPdotnet.3.1-busterslim
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim
# Install ASP.NET Core
RUN aspnetcore_version=3.1.3 
&& curl -SL --output aspnetcore.tar.gz https://dotnetcli.azureedge.net/dotnet/aspnetcore/Runtime/$aspnetcore_version/aspnetcore-runtime-$aspnetcore_version-linux-x64.tar.gz 
&& aspnetcore_sha512='ced0b7c9faaf250fbee2c850e8941d47a17da06fa8e576d57063fa4bbc6c8435de6ab488b683d4a692cd622e24d9592f11608a1b358074167cf0b76024815611' 
&& echo "$aspnetcore_sha512  aspnetcore.tar.gz" | sha512sum -c - 
&& tar -ozxf aspnetcore.tar.gz -C /usr/share/dotnet ./shared/Microsoft.AspNetCore.App 
&& rm aspnetcore.tar.gz

WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "DockerExplained.dll"]``

可以在这里找到aspnet图像的标记列表以及每个图像的操作系统版本。

如果你想使用Ubuntu 20.04,你需要将你的基本映像更改为

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-focal AS base

如果你想要默认的Debian 10,你可以删除图像标签的'buster-slim'部分,这样你就可以得到

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base

正如你在列表中看到的,3.13.1-buster-slim是相同的图像,所以如果你使用3.1,你将得到与你现在使用的相同的图像。

编辑:Dockerfile解释,build/runtime

在这里你创建了一个名为base的临时图像,它基于aspnet运行时图像

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80   

然后根据构建解决方案的SDK映像创建一个名为build的临时映像。请注意,这与上面的图像没有任何关系。

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["DockerExplained/DockerExplained.csproj", "DockerExplained/"]
RUN dotnet restore "DockerExplained/DockerExplained.csproj"
COPY . .
WORKDIR "/src/DockerExplained"
RUN dotnet build "DockerExplained.csproj" -c Release -o /app/build  

然后基于临时构建映像

创建一个名为publish的新临时映像
FROM build AS publish
RUN dotnet publish "DockerExplained.csproj" -c Release -o /app/publish

最后你创建最终的图像,你建立在你的"基础"图像从顶部。这里你从'publish'图片中复制已发布的应用。

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerExplained.dll"]

正如你所看到的,最终的图像是由Dockerfile中的前3行+后4行组成的。中间部分是您构建解决方案的地方,并且Dockerfile的这些位不包含在最终映像中。

你可以用更简单的方式构建它,只需要2个步骤:构建和运行/最终。

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["DockerExplained/DockerExplained.csproj", "DockerExplained/"]
RUN dotnet restore "DockerExplained/DockerExplained.csproj"
COPY . .
WORKDIR "/src/DockerExplained"
RUN dotnet publish "DockerExplained.csproj" -c Release -o /app/publish
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS final
WORKDIR /app
EXPOSE 80   
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "DockerExplained.dll"]

相关内容

  • 没有找到相关文章

最新更新