摘要
上周微软发布了NET 9正式版本,我用官网介绍的方式为Gitlab Runner制作包含NET 9环境的映像,才发现Runtie和SDK的版本号差别。
安装Dotnet的Runtime
Dockerfile中语句如下:
# 使用官方脚本安装 .NET SDK 版本
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 5.0.17 --install-dir /usr/share/dotnet --runtime dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 6.0.36 --install-dir /usr/share/dotnet --runtime dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 7.0.20 --install-dir /usr/share/dotnet --runtime dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 8.0.11 --install-dir /usr/share/dotnet --runtime dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 9.0.0 --install-dir /usr/share/dotnet --runtime dotnet
制作映像成功,Gitlab Runner使用这个映像执行Docker in Docker的任务时,报错:
$ dotnet build "modules/********.csproj"
/usr/bin/bash: line 159: dotnet: command not found
为什么要安装SDK?
如果你安装的是 .NET 运行时(Runtime)而不是 .NET SDK,那么 dotnet 命令可能不会可用,因为运行时主要用于运行 .NET 应用程序,而 SDK 提供了开发工具和编译器等。
因此安装虽然安装了Runtime,但是在Gitlab Runner里执行dotnet开头的各种命令,会提示找不到命令。
因此修改Dockerfile语句如下:
# 使用官方脚本安装 .NET SDK 版本
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 5.0.17 --install-dir /usr/share/dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 6.0.36 --install-dir /usr/share/dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 7.0.20 --install-dir /usr/share/dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 8.0.11 --install-dir /usr/share/dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 9.0.0 --install-dir /usr/share/dotnet
使用docker build命令,报错如下:
Step 10/25 : RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 5.0.17 --install-dir /usr/share/dotnet
---> Running in 44fc4f89f1ab
dotnet-install: Attempting to download using primary link https://dotnetcli.azureedge.net/dotnet/Sdk/5.0.17/dotnet-sdk-5.0.17-linux-x64.tar.gz
--2024-11-20 05:25:43-- https://dotnetcli.azureedge.net/dotnet/Sdk/5.0.17/dotnet-sdk-5.0.17-linux-x64.tar.gz
Resolving dotnetcli.azureedge.net (dotnetcli.azureedge.net)... 117.18.232.200, 2606:2800:147:120f:30c:1ba0:fc6:265a
Connecting to dotnetcli.azureedge.net (dotnetcli.azureedge.net)|117.18.232.200|:443... connected.
HTTP request sent, awaiting response... 404 The specified blob does not exist.
2024-11-20 05:25:45 ERROR 404: The specified blob does not exist..
就是无法下载5.0.17的SDK安装包。
解决方案
来到NET 5官网下载,页面截图如下:
发现Runtime时5.0.17,但是却有多个SDK版本。我们安装命令既然是针对SDK了,自然该用SDK的版本号了。
使用最新的版本号,修改了安装NET 5的命令如下:
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 5.0.408 --install-dir /usr/share/dotnet
再执行docker build,一切正常。
在微软官网,安装各个Dotnet最新SDK的命令,调整到位后如下:
# 使用官方脚本安装 .NET SDK 版本
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 5.0.408 --install-dir /usr/share/dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 6.0.428 --install-dir /usr/share/dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 7.0.410 --install-dir /usr/share/dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 8.0.404 --install-dir /usr/share/dotnet
RUN wget -qO- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh | bash /dev/stdin --version 9.0.100 --install-dir /usr/share/dotnet
使用docker build后,push到仓库,Gitlab Runner使用这个映像后,正确执行了dotnet的命令。
黑夜里不停折腾的代码行者。