10

I have an .net core application where I would like to make a SonarBuild inside a container.

So I have a Dockerfile like this:

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

When I try to build this dockerfile I got an error:

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

How can I install dotnet tool from aspnetcore-build image?

Update

Ok, If I use the base image microsoft/dotnet:2.1-sdk I don't get anymore No executable found matching command "dotnet-tool" error, now I get No executable found matching command "dotnet-sonarscanner"

How can I use sonarscanner tool in this scenario?

Rafael Miceli
  • 2,014
  • 6
  • 22
  • 34

2 Answers2

23

As I have read in this thread: https://github.com/dotnet/dotnet-docker/issues/520, we can run a dotnet tool global command inside a container setting the following line:

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

So my final Dockerfile is like:

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"

Hope that helps someone!

Rafael Miceli
  • 2,014
  • 6
  • 22
  • 34
  • The path of dotnet tools is a bit diffrent: `$HOME/.dotnet/tools` Therefore use: `ENV PATH="${PATH}:/${HOME}/.dotnet/tools"` https://learn.microsoft.com/en-us/dotnet/core/tools/global-tools#install-a-global-tool – Paul Jun 29 '22 at 13:42
0
FROM microsoft/aspnetcore-build as build-env

That's the issue. Look at the dockerhub page for this: https://hub.docker.com/r/microsoft/aspnetcore-build/. See how it says that it only supports versions 1.1 and 2.0:

Latest images for 2.1 and newer are now available on microsoft/dotnet. See this link for more details about migrating to 2.1.

dotnet tool is only available in 2.1 and later. That's why doing RUN dotnet tool install --global dotnet-sonarscanner fails. dotnet doesn't know about a dotnet tool command at all. It tries to look for dotnet-tool in your $PATH, as a fallback, but no such thing exists either.

You actual fix was:

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

Because this uses the new 2.1 SDK, which does know of the dotnet tool command.

omajid
  • 14,165
  • 4
  • 47
  • 64
  • Actually, when I just changed the base image I did install dotnet core tool, but couldn't use it. Just install and don't be able to use solve half my problem, maybe I should change the title of the question to "Use dotnet tool in container" to reflect better my problem – Rafael Miceli Aug 24 '18 at 01:55