安装dotnet核心工具Dockerfile

时间:2018-08-23 02:15:50

标签: docker .net-core

我有一个.net核心应用程序,我想在容器内制作SonarBuild。

所以我有一个像这样的Dockerfile:

FROM microsoft/aspnetcore-build as build-env

WORKDIR /src

COPY question-metrics-api/question-metrics-api.csproj question-metrics-api/
COPY question-metrics-data/question-metrics-data.csproj question-metrics-data/
COPY question-metrics-domain/question-metrics-domain.csproj question-metrics-domain/
COPY tests/question-metrics-domain-tests/question-metrics-domain-tests.csproj tests/question-metrics-domain-tests/

RUN dotnet restore tests/question-metrics-domain-tests
RUN dotnet restore question-metrics-api/question-metrics-api.csproj

COPY . .

#Run tests
RUN dotnet test tests/question-metrics-domain-tests/question-metrics-domain-tests.csproj

#Begin Sonar
RUN dotnet tool install --global dotnet-sonarscanner
RUN dotnet sonarscanner begin /k:"question-metrics-api" /d:sonar.host.url="http://sonarqube:9000" /d:sonar.login="7ed84e09a17a31e783fa8522d876e27fe4624977"
RUN dotnet build
RUN dotnet sonarscanner end /d:sonar.login="7ed84e09a17a31e783fa8522d876e27fe4624977"
#End Sonar

RUN dotnet publish question-metrics-api/question-metrics-api.csproj -c Release -o publish

FROM microsoft/aspnetcore as runtime-env

COPY --from=build-env src/question-metrics-api/publish .
EXPOSE 80
ENTRYPOINT [ "dotnet", "question-metrics-api.dll" ]

当我尝试构建此dockerfile时,出现错误:

Step 11/19 : RUN dotnet tool install --global dotnet-sonarscanner
 ---> Running in 78719975f3b0
No executable found matching command "dotnet-tool"

如何从aspnetcore-build映像安装dotnet工具?

更新

好吧,如果我使用基本映像microsoft / dotnet:2.1-sdk,我再也不会遇到No executable found matching command "dotnet-tool"错误,现在我得到了No executable found matching command "dotnet-sonarscanner"

在这种情况下如何使用声纳扫描仪工具?

2 个答案:

答案 0 :(得分:5)

正如我在以下线程中所读的:https://github.com/dotnet/dotnet-docker/issues/520,我们可以在设置以下行的容器内运行dotnet工具全局命令:

ENV PATH="${PATH}:/root/.dotnet/tools"

所以我的最终Dockerfile是这样的:

FROM microsoft/dotnet:2.1-sdk as build-env

WORKDIR /src

COPY question-metrics-api/question-metrics-api.csproj question-metrics-api/
COPY question-metrics-data/question-metrics-data.csproj question-metrics-data/
COPY question-metrics-domain/question-metrics-domain.csproj question-metrics-domain/
COPY tests/question-metrics-domain-tests/question-metrics-domain-tests.csproj tests/question-metrics-domain-tests/

RUN dotnet restore tests/question-metrics-domain-tests
RUN dotnet restore question-metrics-api/question-metrics-api.csproj

COPY . .

#Run tests
RUN dotnet test tests/question-metrics-domain-tests/question-metrics-domain-tests.csproj

#Begin Sonar
RUN dotnet tool install -g dotnet-sonarscanner

ENV PATH="${PATH}:/root/.dotnet/tools"

RUN dotnet sonarscanner begin /k:"question-metrics-api" /d:sonar.host.url="http://sonarqube:9000" /d:sonar.login="7ed84e09a17a31e783fa8522d876e27fe4624977"
RUN dotnet build
RUN dotnet sonarscanner end /d:sonar.login="7ed84e09a17a31e783fa8522d876e27fe4624977"

希望对某人有帮助!

答案 1 :(得分:0)

FROM microsoft/aspnetcore-build as build-env

这就是问题所在。在dockerhub页面上查看以下内容:https://hub.docker.com/r/microsoft/aspnetcore-build/。看看它说它只支持1.1和2.0版本:

  

Microsoft / dotnet上提供了2.1及更高版本的最新图像。有关迁移到2.1的更多详细信息,请参见此链接。

dotnet tool仅在2.1及更高版本中可用。这就是为什么RUN dotnet tool install --global dotnet-sonarscanner失败的原因。 dotnet根本不了解dotnet tool命令。它会尝试在您的$ PATH中寻找dotnet-tool作为后备,但也不存在这种情况。

您的实际解决方法是:

FROM microsoft/dotnet:2.1-sdk as build-env

因为它使用了新的2.1 SDK,该SDK确实知道dotnet tool命令。