无法从Docker文件运行命令

时间:2020-05-21 13:11:13

标签: powershell docker .net-core chocolatey

我目前正在尝试在笔记本电脑上对.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"]

有人知道吗?

谢谢

1 个答案:

答案 0 :(得分:1)

嗨,欢迎来到Stackoverflow!

我可以看到您的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"]