I'm trying to get Sonar Scanner to analyze a project as part of a dockerfile using the mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1809 image, but it's throwing the following exception when attempting to run dotnet-sonarscanner begin
Unhandled exception. System.IO.IOException: Cannot find local application data directory.
I've verified that the folder it's looking for exists on the container with the following powershell command:
RUN pwsh -Command Get-ChildItem $env:LOCALAPPDATA -Directory
Not sure what I'm missing here. Here's a more complete picture of the dockerfile with example data:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1809 AS build
WORKDIR /app
ARG SONAR_PROJECT_KEY=example-project-key
ARG SONAR_OGRANIZAION_KEY=exapmle-org-key
ARG SONAR_HOST_URL=https://sonarcloud.io
ARG SONAR_TOKEN
# install java
COPY ./install-java.ps1 .
RUN pwsh install-java.ps1
RUN cmd ver
# Install Sonar Scanner
RUN dotnet tool install --tool-path msbuildscanner dotnet-sonarscanner
#ENV PATH="$PATH:/root/.dotnet/tools"
RUN pwsh -Command Get-ChildItem $env:LOCALAPPDATA -Directory
# Start Sonar Scanner
RUN msbuildscanner\dotnet-sonarscanner begin /k:"$SONAR_PROJECT_KEY" /o:"$SONAR_OGRANIZAION_KEY" /d:sonar.host.url="$SONAR_HOST_URL" /d:sonar.login="$SONAR_TOKEN"
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
RUN msbuildscanner\dotnet-sonarscanner end /d:sonar.login="$SONAR_TOKEN"
#Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.9-nanoserver-1809 AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "app.dll"]
Related
I am trying to dockerize an an angular aspnet core 2.2 webapi using the following dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2.105 AS build
WORKDIR /src
# Copy csproj and restore as distinct layers
COPY ["Fightplan_v1/Fightplan_v1.csproj", "Fightplan_v1/"]
COPY ["Fightplan_v1.Autogenerate/Fightplan_v1.Autogenerate.csproj", "Fightplan_v1.Autogenerate/"]
COPY ["Fightplan_v1.Autogenerate.Test/Fightplan_v1.Autogenerate.Test.csproj", "Fightplan_v1.Autogenerate.Test/"]
COPY ["Fightplan_v1.Database/Fightplan_v1.Database.csproj", "Fightplan_v1.Database/"]
COPY ["Fightplan_v1.Helpers/Fightplan_v1.Helpers.csproj", "Fightplan_v1.Helpers/"]
COPY ["Fightplan_v1.Jobs/ConvertImagestoBlob/Fightplan_v1.ConvertImagestoBlob.csproj", "Fightplan_v1.Jobs/ConvertImagestoBlob/"]
COPY ["Fightplan_v1.Models/Fightplan_v1.Models.csproj", "Fightplan_v1.Models/"]
COPY ["Fightplan_v1.Shared/Fightplan_v1.Shared.csproj", "Fightplan_v1.Shared/"]
RUN dotnet restore "Fightplan_v1/Fightplan_v1.csproj"
# Copy everything else and build
COPY . .
WORKDIR "/src/Fightplan_v1"
RUN dotnet build "Fightplan_v1.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Fightplan_v1.csproj" -c Release -o /app
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY --from=build /app .
EXPOSE 80
ENTRYPOINT ["dotnet", "Fightplan_v1.dll"]
The build command used is:
docker build -f .\fp.dockerfile -t test .
The build goes through fine but when I try to run it using:
docker run -p 5100:80 -it test
I get the following error:
I have tried:
Adding -r linux-x64 to the end of publish to define the runtime: RUN dotnet publish "Fightplan_v1.csproj" -c Release -o /app -r linux-x64
Adding <PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest> to my .csproj file
None of the above fixes work unfortunately.
Since this is combined poject of an angular application and a webapi I might be missing some installations/dependencies?
You are copying the runtime files from wrong layer
COPY --from=build /app .
Try to change it to
COPY --from=publish /app .
Also, you probably need to separate out path in build and publish steps:
RUN dotnet build "Fightplan_v1.csproj" -c Release -o /app/build
RUN dotnet publish "Fightplan_v1.csproj" -c Release -o /app/publish
and then copy runtime files from the publish folder
COPY --from=publish /app/publish .
Edit. Basically, you need to copy publish artifacts into runtime, not build. Build will produce deps.json, which will lists all external dependencies, and during runtime this libraries are resolving using NuGet cache, if it is empty you will see an error. To copy dependencies along with project files you need to run publish.
So I assume, that theoretically dotnet build can be omitted, and is using only to discover compilation errors on early stage of docker build.
I know this question was asked but nobody answer it.
I'm building web API on Core 2.2 with JWT token bearer. In token request I added check if user exist on our LDAP.
All working properly on IIS and console app but when I run with docker I'm getting:
"Unable to load DLL 'activeds.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
error.
If user exist all working fine but when user not exist Im checking user status with:
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
Which causing error above.
Im new to docker. I have tried to play with images:
mcr.microsoft.com/dotnet/core/sdk
mcr.microsoft.com/dotnet/core/aspnet
microsoft/aspnetcore-build
In all cases the same result
I have exposed LDAP ports in docker file (or I think I exposed) and still getting the same error.
My current dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
EXPOSE 383
EXPOSE 636
FROM mcr.microsoft.com/dotnet/core/sdk AS build
WORKDIR /src
COPY ["USITWebApi/USITWebApi.csproj", "USITWebApi/"]
RUN dotnet restore "USITWebApi/USITWebApi.csproj"
COPY . .
WORKDIR "/src/USITWebApi"
RUN dotnet build "USITWebApi.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "USITWebApi.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "USITWebApi.dll"]
I'm using Windows containers.
I spent three days trying to solve this. If you have any ideas please help me solve this issue.
Looks that it is a limitation based on the image that you are using as base. If you would like to use LDAP use any server core image based on your host OS. This is what I am doing. This is a example for Windows 2019 Server as host.
FROM mcr.microsoft.com/windows/servercore:ltsc2019 AS base
WORKDIR /app
EXPOSE 80
RUN powershell Set-ExecutionPolicy RemoteSigned
RUN powershell iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
RUN powershell choco install dotnet-6.0-runtime -Y
RUN powershell choco install dotnet-6.0-sdk -y
ENV ASPNETCORE_URLS=http://+80
RUN powershell fsutil behavior set symlinkEvaluation L2L:1 L2R:1 R2R:1 R2L:1
RUN powershell Set-ExecutionPolicy Bypass -Scope Process
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["src/Your.ProjectFile.csproj", "src/Your.Project.Folder/"]
COPY ["./NuGet.Config", "src/Your.Project.Folder"]
RUN dotnet restore "src/Your.Project.Folder/Your.ProjectFile.csproj"
COPY . .
WORKDIR "/src/src/Your.Project.Folder"
RUN dotnet build "Your.ProjectFile.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Your.ProjectFile.csproj" -c Release -o /app/publish
FROM base
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Your.ProjectFile.dll"]
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?
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!
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.
I am completely new to Docker. I am trying to learn how to dockerize .NET Core app based on tutorial.
Whatever I do, I can't dockerize it. I am always stuck on:
RUN dotnet publish -c Release -o out
within dockerfile.
Errors I get are:
C:\Program Files\dotnet\sdk\2.1.200\NuGet.targets(114,5): error : An
error occurred while sending the request. [C:\app\TokenGen.csproj]
C:\Program Files\dotnet\sdk\2.1.200\NuGet.targets(114,5): error :
The server name or address could not be resolved
[C:\app\TokenGen.csproj]
I noticed that he is pointing to
C:\app
even tho my sample project is on
H:\Bitbucket\dockertest\TokenGen
This is my full dockerfile:
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
When I run (inside Dockerfile) RUN dotnet publish --output /app/ process/build is run successfully.
But when I run with Release: RUN dotnet publish --output /app/ -c Release I get error in GitLab console:
C:\source\Web.Core\Web.Core.csproj(34,5): error MSB3030: Could not copy the file "C:\source\Web.Core\\bin\Debug\netcoreapp2.0\Web.Core.dll" because it was not found.
Web.Library.Tests -> C:\app\
The command 'cmd /S /C dotnet publish --output /app/ -c Release' returned a non-zero code: 1
Does anybody have any clue, why build process failed with -c Release? Same command dotnet publish --output /app/ -c Release run successfully from Windows PowerShell.
My GitLab runner and Docker are installed on my Windows 10 Pro.
What is strange is that it tries to copy from bin\Debug. I would expect from bin\Release. I understand file can't be found in Debug. But I can't figure it out, what I messed up in Dockerfile.
Dockerfile:
# Stage 1
FROM microsoft/aspnetcore-build:2.0 AS builder
WORKDIR /source
# caches restore result by copying csproj file separately
COPY Web.sln ./src/aspdocker/Web.sln
COPY Web.Library/Web.Library.csproj ./src/aspdocker/Web.Library/
COPY Web.Core/Web.Core.csproj ./src/aspdocker/Web.Core/
COPY Web.Hosting/Web.Hosting.csproj ./src/aspdocker/Web.Hosting/
COPY Web.Library.Tests/Web.Library.Tests.csproj ./src/aspdocker/Web.Library.Tests/
COPY Web.Core.Tests/Web.Core.Tests.csproj ./src/aspdocker/Web.Core.Tests/
# caches restore result by copying csproj file separately
RUN dotnet restore ./src/aspdocker/Web.sln
# copies the rest of your code
COPY . .
RUN dotnet publish --output /app/ -c Release
#RUN dotnet publish --output /app/
# Stage 2
#FROM microsoft/aspnetcore:1.1.2
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=builder /app .
EXPOSE 5000/tcp
ENTRYPOINT ["dotnet", "Web.dll"]