无法从docker文件运行命令

问题描述 投票:0回答:1

我目前正在尝试在笔记本电脑上对.net核心API进行Docker化,但是在构建Docker文件时,我遇到了一些问题。

[尝试执行choco或wget之类的命令时遇到问题。它说它们未被识别,但是我确实安装了它们并作为变量添加到我的环境中。当我尝试在终端中独立执行它们时,它们确实起作用。

这里是我的剧本:

FROM microsoft/dotnet:2.2-sdk AS dotnet-builder

ARG nuget_pat

# Set environment variables
ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS '{"endpointCredentials":[{"endpoint":"https://isirac.pkgs.visualstudio.com/_packaging/RentacarMicroserviceNuget/nuget/v3/index.json","username":"NoRealUserNameAsIsNotRequired","password":"'${nuget_pat}'"}]}'

RUN choco install wget

# Get and install the Artifact Credential provider
RUN wget -O - https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh  | bash

# Restore your nugets from nuget.org and your private feed.
# RUN dotnet restore -s "https://isirac.pkgs.visualstudio.com/_packaging/RentacarMicroserviceNuget/nuget/v3/index.json" -s "https://api.nuget.org/v3/index.json" "Suzuki.csproj"

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1909 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1909 AS build
WORKDIR "/src/Suzuki/Suzuki/"
COPY ["*.csproj", "./"]
# COPY --from=nuget-config NuGet.config ./
RUN dotnet restore --interactive "Suzuki.csproj" -s "https://isirac.pkgs.visualstudio.com/_packaging/RentacarMicroserviceNuget/nuget/v3/index.json" -s "https://api.nuget.org/v3/index.json"
COPY . .
WORKDIR "/src/Suzuki/Suzuki/"
RUN dotnet build "Suzuki.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Suzuki.csproj" -c Release -o /app/publish

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

有人知道吗?

感谢

powershell docker .net-core chocolatey
1个回答
1
投票
我可能会看到您的Dockerfile有一些问题,第一个应该与您的问题有关。您使用的是choco,但您之前从未安装过。

您可以添加RUN来添加它:

RUN powershell -Command \ iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')); \ choco feature disable --name showDownloadProgress

我看到的另一个问题是您如何使用Docker的多阶段构建。它不是bug或类似的东西,但它可能会更好,更容易阅读。

每次添加FROM指令,您都会启动一个新图像,并且能够复制以前图像中的某些文件。

在您的Dockerfile中,您有一个未真正使用的步骤:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1909 AS base WORKDIR /app EXPOSE 80 EXPOSE 443

从这个阶段开始,您什么也没复制,什么也不做。您稍后将重用它,但是我们可能需要仔细阅读以查找步骤,因为我们可能会错过它。稍后,我将其与最终步骤合并。

我不理解的另一个阶段:

FROM build AS publish RUN dotnet publish "Suzuki.csproj" -c Release -o /app/publish

为什么不直接在build图像上运行命令?您可以在publish图像中使用文件,因为您是从build开始复制它们的,但是在这种情况下,您可以继续直接使用build

最后是最后一个阶段:

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

如果我们以前使用基本图像声明,则只需向其中添加一些指令,然后重复一条指令即可:WORKDIR是两个位置中具有相同值的声明。我认为,最好的办法是最后一个阶段将两者合并,如下所示:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1909
EXPOSE 80
EXPOSE 443

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

您无需命名,因为以后将不会使用它。

所以,如果我们赶上了,这就是我要使用的Dockerfile:

FROM microsoft/dotnet:2.2-sdk AS dotnet-builder ARG nuget_pat # Set environment variables ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS '{"endpointCredentials":[{"endpoint":"https://isirac.pkgs.visualstudio.com/_packaging/RentacarMicroserviceNuget/nuget/v3/index.json","username":"NoRealUserNameAsIsNotRequired","password":"'${nuget_pat}'"}]}' RUN powershell -Command \ iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')); \ choco feature disable --name showDownloadProgress; \ choco install wget # Get and install the Artifact Credential provider RUN wget -O - https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh | bash # Restore your nugets from nuget.org and your private feed. # RUN dotnet restore -s "https://isirac.pkgs.visualstudio.com/_packaging/RentacarMicroserviceNuget/nuget/v3/index.json" -s "https://api.nuget.org/v3/index.json" "Suzuki.csproj" WORKDIR "/src/Suzuki/Suzuki/" COPY *.csproj ./ # COPY --from=nuget-config NuGet.config ./ RUN dotnet restore --interactive Suzuki.csproj -s "https://isirac.pkgs.visualstudio.com/_packaging/RentacarMicroserviceNuget/nuget/v3/index.json" -s "https://api.nuget.org/v3/index.json" COPY . . RUN dotnet build "Suzuki.csproj" -c Release -o /app/build RUN dotnet publish "Suzuki.csproj" -c Release -o /app/publish FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1909 EXPOSE 80 EXPOSE 443 WORKDIR /app COPY --from=dotnet-builder /app/publish . ENTRYPOINT ["dotnet", "Suzuki.dll"]

© www.soinside.com 2019 - 2024. All rights reserved.