制作 windows容器基础镜像+dotnet7 的自定义基础镜像

前言:
通常我们能够下载到的windows dotnet 基础镜像都是基于 linux 制作而成的
例如:https://hub.docker.com/_/microsoft-dotnet-sdk/

制作 windows容器基础镜像+dotnet7 的自定义基础镜像

思考:
开始我想到的是先下载容器基础镜像,然后将下载.net 7 的exe安装包,两者相加就可以完成,但是,最终执行的时候发现,无论如何都无法执行exe文件,因此制作安装失败

https://hub.docker.com/_/microsoft-dotnet-sdk/
这个地址直接显示了制作 dotnet 7 + windowsservercore-ltsc2019 的Dockerfile 地址 !!!!!
image
image

在网络允许的情况下,直接根据以上的Dockerfile 制作镜像是没有问题的
但是,如果能够提前下载好需要的安装包,再进行构建,会更快
以下是我调整过得Dockerfile

# escape=`

ARG REPO=mcr.microsoft.com/dotnet/aspnet
FROM $REPO:7.0.2-windowsservercore-ltsc2019

####    Dockerfile 模板地址
####    https://github.com/dotnet/dotnet-docker/blob/main/src/sdk/7.0/windowsservercore-ltsc2019/amd64/Dockerfile
##########################################################################################
################################################ 根据下面的 “注释处理”内容,提前准备好安装包 
COPY PowerShell.Windows.x64.7.3.1.nupkg  PowerShell.Windows.x64.7.3.1.nupkg
COPY dotnet-sdk-7.0.102-win-x64.zip           dotnet.zip
COPY MinGit-2.39.0.2-64-bit.zip                   mingit.zip


ENV `
    # Unset ASPNETCORE_URLS from aspnet base image
    ASPNETCORE_URLS= `
    # Do not generate certificate
    DOTNET_GENERATE_ASPNET_CERTIFICATE=false `
    # Do not show first run text
    DOTNET_NOLOGO=true `
    # SDK version
    DOTNET_SDK_VERSION=7.0.102 `
    # Enable correct mode for dotnet watch (only mode supported in a container)
    DOTNET_USE_POLLING_FILE_WATCHER=true `
    # Skip extraction of XML docs - generally not useful within an image/container - helps performance
    NUGET_XMLDOC_MODE=skip `
    # PowerShell telemetry for docker image usage
    POWERSHELL_DISTRIBUTION_CHANNEL=PSDocker-DotnetSDK-WindowsServerCore-ltsc2019

# Download MinGit
RUN powershell -Command " `
        $ErrorActionPreference = 'Stop'; `
        $ProgressPreference = 'SilentlyContinue'; `
        `
########注释处理##########        Invoke-WebRequest -OutFile mingit.zip https://github.com/git-for-windows/git/releases/download/v2.39.0.windows.2/MinGit-2.39.0.2-64-bit.zip; `
########注释处理##########        $mingit_sha256 = '771e7bef1b672e3f63b18b8c4a62d626c8f47c41390a745f313758c0b6ae4d63'; `
########注释处理##########        if ((Get-FileHash mingit.zip -Algorithm sha256).Hash -ne $mingit_sha256) { `
########注释处理##########            Write-Host 'CHECKSUM VERIFICATION FAILED!'; `
########注释处理##########            exit 1; `
########注释处理##########        }; `
        mkdir $Env:ProgramFiles\MinGit; `
        tar -oxzf mingit.zip -C $Env:ProgramFiles\MinGit; `
        Remove-Item -Force mingit.zip"

RUN powershell -Command " `
        $ErrorActionPreference = 'Stop'; `
        $ProgressPreference = 'SilentlyContinue'; `
        `
        # Retrieve .NET SDK
########注释处理##########        Invoke-WebRequest -OutFile dotnet.zip https://dotnetcli.azureedge.net/dotnet/Sdk/$Env:DOTNET_SDK_VERSION/dotnet-sdk-$Env:DOTNET_SDK_VERSION-win-x64.zip; `
########注释处理##########        $dotnet_sha512 = '841ca296ae1d4f6efed6516dec0594c44449129113e05351b3ca8a92b573ee5d333eaabb577169f5e32c12ee977f24ac0ee3d9a30fbb9a2b0b7c08b8dd414e85'; `
########注释处理##########        if ((Get-FileHash dotnet.zip -Algorithm sha512).Hash -ne $dotnet_sha512) { `
########注释处理##########            Write-Host 'CHECKSUM VERIFICATION FAILED!'; `
########注释处理##########            exit 1; `
########注释处理##########       }; `
        tar -oxzf dotnet.zip -C $Env:ProgramFiles\dotnet ./LICENSE.txt ./ThirdPartyNotices.txt ./packs ./sdk ./sdk-manifests ./templates ./shared/Microsoft.WindowsDesktop.App; `
        Remove-Item -Force dotnet.zip; `
        `
        # Install PowerShell global tool
        $powershell_version = '7.3.1'; `
########注释处理##########        Invoke-WebRequest -OutFile PowerShell.Windows.x64.$powershell_version.nupkg https://pwshtool.blob.core.windows.net/tool/$powershell_version/PowerShell.Windows.x64.$powershell_version.nupkg; `
########注释处理##########        $powershell_sha512 = '1ebf84206e55ba61d1d10ceb042ca6c841da59f66731d54e945a8a3e190792a3fec23f23a01482ab95690cab9084a96e902772959f0035326a249ebdaf5e5332'; `
########注释处理##########        if ((Get-FileHash PowerShell.Windows.x64.$powershell_version.nupkg -Algorithm sha512).Hash -ne $powershell_sha512) { `
########注释处理##########            Write-Host 'CHECKSUM VERIFICATION FAILED!'; `
########注释处理##########            exit 1; `
########注释处理##########        }; `
        & $Env:ProgramFiles\dotnet\dotnet tool install --add-source . --tool-path $Env:ProgramFiles\powershell --version $powershell_version PowerShell.Windows.x64; `
        & $Env:ProgramFiles\dotnet\dotnet nuget locals all --clear; `
        Remove-Item -Force PowerShell.Windows.x64.$powershell_version.nupkg; `
        Remove-Item -Path $Env:ProgramFiles\powershell\.store\powershell.windows.x64\$powershell_version\powershell.windows.x64\$powershell_version\powershell.windows.x64.$powershell_version.nupkg -Force;"

RUN setx /M PATH "%PATH%;C:\Program Files\powershell;C:\Program Files\MinGit\cmd"

# Trigger first run experience by running arbitrary cmd
RUN dotnet help


######### 构建命令 docker build -t docker.365xs.cn/library/windowsservercore-ltsc2019-vc-dotnet:7 .
######### 推送命令 docker push docker.365xs.cn/library/windowsservercore-ltsc2019-vc-dotnet:7
posted @ 2023-02-03 10:36  海兵的正义  阅读(275)  评论(0编辑  收藏  举报