I have a solution which contains two projects which has the following structure:
The project "TestWebApp" is a xUnit testproject and contains the software tests. The project "WebApplication" the software which should be tested with the testproject.
The software works fine but by now i would like to build a docker container which should be included in a gitlab ci-cd pipeline. My question contains two steps:
Should i build a container which contains the business application ("WebApplication") and the software test ("TestWebApp")? If this is the case how could i do that? How can i run the software test in the container? My Dockerfile looks like the following
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
COPY ["TestWebApp/TestWebApp.csproj", "TestWebApp/"]
# ------------------SOFTWARE TEST-------------------------------------------------------------
RUN dotnet restore "TestWebApp/TestWebApp.csproj"
RUN dotnet build "TestWebApp/TestWebApp.csproj" -c Release
RUN dotnet test --no-build -c Release --results-directory /testresults --logger "trx;LogFileName=test_result.xml" TestWebApp/TestWebApp.csproj
# ------------------WEB APP-------------------------------------------------------------
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR "/src/WebApplication1"
RUN dotnet build "WebApplication1.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
COPY --from=test /testresults .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]
If i try it like the following dockerfile i get this error:
------
> FROM docker.io/library/test:latest:
------
failed to solve with frontend dockerfile.v0: failed to build LLB: failed to load cache key: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
If I shouldn't build a container with business application and test - what would be the correct way?
It's fine to build them both in the docker container -- in fact, you'd want to do that; because you want your tests to run on every build of your CI/CD pipeline. The trick is they'll be in your initial docker image; but not in the one that gets deployed; and your Dockerfile code shows you're already doing that with the final.
The steps in your docker image should look a bit like this:
Build Project
Build Tests
Run Tests
Copy Deployable objects to deployable docker image
That final artifact, the deployable artifacts (without your tests) is what gets deployed; but this ensures your tests get run on every build.
So inyour answer, the only thing that's missing (at least from my eyeballing it) is a dotnet test for your test project between the build and final.
Related
Objective -> I am currently trying to integrate Dapr with a small sample project provided by the .Net project templates. At the end of my test cases, I would like to use Docker Compose to launch multiple containers for a web API and a web app to communicate with each other using the Dapr sidecars.
Problem -> Whenever I run docker compose up on the sample application, the container fails to be created and is stopped by this error:
> [build 4/7] RUN dotnet restore "MyBackEnd/MyBackEnd.csproj":
#0 0.945 Determining projects to restore...
#0 7.179 /src/MyBackEnd/MyBackEnd.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
#0 7.202 Failed to restore /src/MyBackEnd/MyBackEnd.csproj (in 6.09 sec).
Steps to Reproduce ->
The following are the steps I took with this project:
Ran mkdir sample_project && cd sample_project
Ran dotnet new webapi -o MyBackEnd followed by cd MyBackEnd
Ran touch Dockerfile && cd .. && touch docker-compose.yml
I then put this in my Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
RUN apt-get update && apt-get install -y libcurl4
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["MyBackEnd/MyBackEnd.csproj", "MyBackEnd/"]
RUN dotnet restore "MyBackEnd/MyBackEnd.csproj"
COPY . .
WORKDIR "/src/MyBackEnd"
RUN dotnet build "MyBackEnd.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyBackEnd.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyBackEnd.dll"]
And this was what I did with my Docker Compose YAML file:
version: '3.4'
services:
mybackend:
image: ${DOCKER_REGISTRY-}mybackend
build:
context: .
dockerfile: MyBackEnd/Dockerfile
ports:
- "3000:80"
Finally, I ran docker compose up to arrive at the issue
Additional Information -> I am not running this on a Windows system. I am using Ubuntu 20, and thus, I do not have access to Visual Studio to handle the launching and configuration for me. I am aware that this is a heavily replicated question, but I have yet to see anything that specifically applies to users that do not have access to Visual Studio or on Ubuntu. Thanks!
I have a dotnet5 Console application that is inside a solution and has dependencies on other sibling projects. I create the Docker file with visual studio tools and this is the Docker file:
FROM mcr.microsoft.com/dotnet/runtime:5.0-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["src/MyProject/MyProject.Launcher/MyProject.Launcher.csproj", "src/MyProject/MyProject.Launcher/"]
COPY ["src/MyProject/MyProject.DataAccess/MyProject.DataAcess.csproj", "src/MyProject/MyProject.DataAccess/"]
COPY ["src/MyProject/MyProject.Service/MyProject.Service.csproj", "src/MyProject/MyProject.Service/]
COPY ["src/Domain/MyProject.Domain.Model/MyProject.Domain.Model.csproj", "src/Domain/MyProject.Domain.Model/"]
RUN dotnet restore "src/MyProject/MyProject.Launcher/MyProject.Launcher.csproj"
COPY . .
WORKDIR "/src/src/MyProject/MyProject.Launcher"
RUN dotnet build "MyProject.Launcher.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyProject.Launcher.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.Launcher.dll"]
building image is ok and it added normally to my local image registry but when I try to run this image and create a container I am getting this error:
It was not possible to find any compatible framework version The
framework 'Microsoft.AspNetCore.App', version '5.0.0' was not found.
No frameworks were found.
You can resolve the problem by installing the specified framework
and/or SDK.
Use this as your base image:
FROM mcr.microsoft.com/dotnet/aspnet:5.0.0-buster-slim AS base
I Think You should check docker hub and search for .NET 5 SDK then install that image on your container then its will be fixed :
I Found Dotnet 3.1 SDK but try to find dotnet 5 SDK
I think after installation this error will be fix
Sorry For my Bad English
:)
I am trying to build docker images for my asp.net 3.1 services. When I build self-contained I can use the mcr.microsoft.com/dotnet/core/runtime-deps base image just fine. However to improve build speed I am trying to use the mcr.microsoft.com/dotnet/core/runtime or aspnet as my base image and copy the no-self-contained (framework dependent) publish output (from an earlier stage in docker build).
However when I try to run the container I see A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '/usr/share/dotnet'.
I can reproduce this using the webapi sample project and the following Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic as build
WORKDIR /src
COPY . .
RUN dotnet restore --runtime ubuntu.18.04-x64
RUN dotnet build --no-restore -c Release --runtime ubuntu.18.04-x64
RUN dotnet publish --no-restore --no-build -c Release -r ubuntu.18.04-x64 -o publish --self-contained false ./app.csproj
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic
WORKDIR /app
COPY --from=build /src/publish/. /app/
CMD [ "./app" ]
Also I can see the libhostpolicy.so bin does exist in /usr/share/dotnet/shared/Microsoft.NETCore.App/3.1.3/libhostpolicy.so
Can you see what I am doing wrong here?
I am still unsure what the cause of the error was but I can produce a container the same size as the problematic one that works properly by omitting the runtime from the dotnet commands as follows:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic as build
WORKDIR /src
COPY . .
RUN dotnet restore
RUN dotnet build --no-restore -c Release
RUN dotnet publish --no-restore --no-build -c Release -o publish ./app.csproj
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic
WORKDIR /app
COPY --from=build /src/publish/. /app/
CMD [ "./app" ]
If anyone knows why passing the RID caused a runtime dependency error I would be interested to know but for now I am happy
I'm trying to build the most basic web Api application using VS2019 along with docker. Basically it is just the demo app provided by VS. I'm ending up with below error:
Severity Code Description Project File Line Suppression State
Error CTC1014 Docker command failed with exit code 1.
hcsshim::PrepareLayer - failed failed in Win32: Incorrec
Below my dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 8080
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["RegexTesterApi/RegexTesterApi.csproj", "RegexTesterApi/"]
RUN dotnet restore "RegexTesterApi/RegexTesterApi.csproj"
COPY . .
WORKDIR "/src/RegexTesterApi"
RUN dotnet build "RegexTesterApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "RegexTesterApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "RegexTesterApi.dll"]
Well this is the auto generated dockerfile. But it looks pretty ok to me (also I'm new to docker).
What is actually causing the error.
I ran into the very same error (CTC1014) in the past. I found out that my project was being run in "Release" mode, when it should have been running in "Debug" mode. So I would like to suggest this as a workaround.
Here's how you change it in VS2019
Of course, running your application in Release mode shouldn't be a problem. So I assumed it must have been related to some optimization employed by most release build configurations, in conflict with Docker. Not exactly sure which one, though.
Cheers! o/
I faced this issue too, but with .net core 2.1. I'm not sure why it's working but, If this solution is available for you then try the following:
Restart Docker Desktop (This solution is offered in the output window)
Change the docker file.
I replaced this
FROM mcr.microsoft.com/dotnet/runtime:2.1 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:2.1 AS build
WORKDIR /src
to
FROM mcr.microsoft.com/dotnet/core/runtime:2.1-alpine AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:2.1-alpine AS build
WORKDIR /src
Then debug started to work fine even after changing the docker file to start variant, including stops on breakpoints. And my configuration has one additional difference:
I use Debug argument in the docker file and use Debug in Solution configuration
RUN dotnet build "DebugLinux.csproj" -c Debug -o /app/build
This error migh be raised when the dockerfile in your project don't match the Target Framework in the project settings.
Just delete your dockerfile and create a new one and this will fix the problem if this is the issue.
I've got a dotnet core project running AspNetCore webserver. I use a couple of other DLLs which is are fairly simple class libaries.
I can download the repository from git onto my windows PC, go in and run:
dotnet restore
dotnet run
And everything works fine.
However if I do the same thing in a docker container based on microsoft/aspnetcore-build:1.0.7, I get the following error on an HTTP PUT:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLBKHRVH7OND": An unhandled exception was thrown by the application.
System.IO.FileNotFoundException: Could not load file or assembly 'KolData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
Now the file Koldata.dll does exist in the git repository and it's there in the bin/Debug/netcoreapp1.1 folder.
I can re-create the error in Windows by deleting the KolData.dll file in the build directory. So it seems to be that dotnet core on Linux cannot see that file, and I'm unsure why.
I've even tried replacing the DLL with a version built on the machine from source, and it still brings the same error.
One solution
I managed to get it working by changing the csproj file's target framework from:
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
to:
<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
This feels a bit strange since KolData.dll is running on 1.1
But now it runs without the error.
You have to create Dockerfile and build docker image.
EDIT:
An example of what Dockerfile should look like. The following file builds Visual Studio project and creates docker image.
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY WebApplication/WebApplication.csproj WebApplication/
RUN dotnet restore
COPY . .
WORKDIR /src/WebApplication
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApplication.dll"]
If you have already built your application include .dll in container with Dockerfile:
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "application.dll"]
Build and run your app:
docker build -t application
docker run -d -p 8000:80 application
Building Docker Images for .NET Core Applications