0

I'm currently trying to dockerize a .net core API on my laptop but when I'm building the docker file I have some issue.

I have an issue when it's trying to execute commands like choco or wget. It says that they are not recognized, I did install them though and added as variables into my environment. They do work when I try to execute them independently in a terminal.

Here my script:

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"]

Does anyone have a clue?

thanks

Stuart
  • 6,630
  • 2
  • 24
  • 40

1 Answers1

1

Hi and welcome on Stackoverflow!

I could see some issues with your Dockerfile and the first one should be related to your issue. You use choco but you didn't install it before.

You could add a RUN to add it:

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

Another problem I see is how you use the multistage build of Docker. It's not a bug or something like that, but it could be better and easier to read.

Everytime you add a FROM instruction, you start a new image and you have the ability to copy some files from previous images.

In your Dockerfile, you have one step that is not really used:

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

From this stage, you didn't copy anything and do nothing. You will reuse it later but we need to read up to find the step as we may miss it. Later I will merge it with the final steps.

Another stage I don't understand:

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

Why not running the command directly on the build image? You have your file available in the publish image because you start from build that copy them, but, in this case, you can continue to use build directly.

And finally, the last stage:

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

If we use the base image declare previously, you just add some instructions to it and have a duplicate one: WORKDIR is the declare in the two place with the same value. I think, the best is to have a final stage that merge the two and be like this:

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"]

You don't need to name it as you will not use it after.

So, if we catch up, this is the Dockerfile I would purpose:

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"]
jmaitrehenry
  • 2,190
  • 21
  • 31