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.
Related
I'm experimenting Visual Studio Code Remote.
I want to use SonarScanner for code quality analysis. I've followed the guides and installed the tool on host but when I try to use it from the container, I get:
# dotnet sonarscanner -h
No executable found matching command "dotnet-sonarscanner"
On host:
$ dotnet sonarscanner -h
SonarScanner for MSBuild 4.7.1
Using the .NET Core version of the Scanner for MSBuild
Usage:
SonarScanner.MSBuild [begin|end] /key:project_key [/name:project_name] [/version:project_version] [/d:sonar.key=value] [/s:settings_file]
When executing the begin phase, at least the project key must be defined.
Other properties can dynamically be defined with '/d:'. For example, '/d:sonar.verbose=true'.
A settings file can be used to define properties. If no settings file path is given, the file SonarQube.Analysis.xml in the installation directory will be used.
Only the token should be passed during the end phase, if it was used during the begin phase.
this post can help you!
Install dotnet core tool Dockerfile
we can run a dotnet tool global command inside a container setting the following line:
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"
Got It! I've found an article explaining it.
It was just including this instructions on Dockerfile.
ENV SONNARSCANNER_VERSION 2.3.38
RUN dotnet tool install --global dotnet-sonarscanner --version $SONNARSCANNER_VERSION
ENV PATH="/root/.dotnet/tools:${PATH}"
ENTRYPOINT ["dotnet-sonarscanner"]
I am trying to build image for .net core 3.1 web api project but unable to build image
i get stuck at restoring project packages via dotnet restore
i get stuck at when i use dotnet build command ( does not find the solution or project file)
Directory Structure
https://i.stack.imgur.com/J7sMq.png
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as base
#FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
#copy all project and solution files
COPY ./SSFS.Service/.csproj ./SSFS.Service/
COPY ./SSF.EDM/.csproj ./SSF.EDM/
COPY ./API/.csproj ./API/
COPY ./.sln ./
WORKDIR /app/API
RUN dotnet restore "SSFAPI.csproj"
#copy rest of files
COPY . .
WORKDIR /app
#build the project to restore packages
RUN dotnet build --source "./SSFAPI.sln" -c Release -o /publish
#public the project to a folder
RUN dotnet publish --source "SSFAPI.sln" -c Release -o /publish
FROM base AS final
EXPOSE 80
WORKDIR /app
COPY --from=base /app/publish .
ENTRYPOINT ["dotnet", "SSFAPI.dll"]
output of above is as below
https://i.stack.imgur.com/xqoKv.png
If i build the project using dotnet command then out is as shown below
Output is as below
https://i.stack.imgur.com/t9bV5.png
The Dockerfile is set up to be run from the solution root. You're probably running docker build from the project directory (i.e. where the Dockerfile actually is).
I am trying to dockerize my Angular ASP.NET Core WebAPI.
I have the following dockerfile created:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2.105 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY src/Fightplan_v1/Fightplan_v1.csproj ./
# Copy everything else and build
COPY . ./
RUN dotnet restore
RUN dotnet publish -c Release -o /app
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app .
ENTRYPOINT ["dotnet", "Fightplan_v1.dll"]
The structure of my project is as follows:
The WebApi project is located inside the "src/Fightplan_v1" folder.
I am able to build my image fine using the following command:
docker build -f fp.dockerfile -t test .
When I try to run the image using:
docker run -it test
I get the following error:
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
The project is build on dotnet core 2.2.105 so what this ofcourse tells me is that the image does not have the necessary dotnet core sdk installed. But as you can see in the dockerfile the sdk version I am downloading is 2.2.105.
I read here that the ENTRYPOINT is case sensitive and I have checked this several times now.
If I do a dotnet publish locally and go into the bin/Debug/netcoreapp2.2 I will find a DLL file called "Fightplan_v1.dll". Which is the name I assume I need to add to ENTRYPOINT?
I might just be missing some other steps here but I am not sure what.
Any help would be much appreciated!
I think your docker file should be like this
ENTRYPOINT ["Fightplan_v1.dll"]
Or if this not work try this
CMD ASPNETCORE_URLS=http://*:$PORT dotnet Fightplan_v1.dll
I was missing a COPY . . in the dockerfile after dotnet restore. I guess I was missing some .cs files in order for it to be compiled correctly.
The working dockerfile ended up looking like this:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2.105 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY src/Fightplan_v1/Fightplan_v1.csproj ./
# Copy everything else and build
COPY . ./
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app .
ENTRYPOINT ["dotnet", "Fightplan_v1.dll"]
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.
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"]