How to add a third-party DLL to a DockerFile? C# - docker

I have a multi layered project and a third party DLL file and I want to dockerize them. I added a dockerfile into my webApi project and tried to build it but this error occurred:
3>Step 7/22 : COPY ["D:/MultiLayeredProject/output/MultiLayeredProject/PluginProject.dll", "MultiLayeredProject/"]
3>COPY failed: stat /var/lib/docker/tmp/docker-builder959745697/D:/MultiLayeredProject/output/MultiLayeredProject/PluginProject.dll: no such file or directory
3>C:\Users~~\source\repos\MultiLayeredProject\MultiLayeredProject\Dockerfile : error CTC1014: Docker command failed with exit code 1.
3>C:\Users~~\source\repos\MultiLayeredProject\MultiLayeredProject\Dockerfile : error CTC1014: COPY failed: stat /var/lib/docker/tmp/docker-builder959745697/D:/MultiLayeredProject/output/MultiLayeredProject/PluginProject.dll: no such file or directory
How can I add an external DLL to the dockerfile? My dockerfile is like below:
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["D:/MultiLayeredProject/output/MultiLayeredProject/PluginProject.dll", "MultiLayeredProject/"]
COPY ["MultiLayeredProject/MultiLayeredProject.csproj", "MultiLayeredProject/"]
COPY ["AppService/AppService.csproj", "AppService/"]
COPY ["Core/Core.csproj", "Core/"]
RUN dotnet restore "MultiLayeredProject/MultiLayeredProject.csproj"`
COPY . .
WORKDIR "/src/MultiLayeredProject"
RUN dotnet build "MultiLayeredProject.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MultiLayeredProject.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MultiLayeredProject.dll"]

As I understand, your third-part dll is a part of project folder. So, instead of using absolute path to file, try to use relative path, like:
COPY ["output/MultiLayeredProject/PluginProject.dll", "MultiLayeredProject/"]
When you call docker build, docker cli sends your project structure (context) to docker daemon (inside Linux OS) and there is no D:/... path.

Related

Dockerfile: Getting failed to compute cache key: "/nginx.conf" not found: not found

I'm working on a Blazor Wasm (ASP.Net Core hosted consisting on the usual 3 projects Client, Server and Shared) app that I want to deploy to Linux using docker.
I'm copying a nginx.conf file to the Server project root folder but when trying to publish to Azure App Service Containers I'm getting:
failed to compute cache key: "/nginx.conf" not found: not found
This is my Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["MyLocalShop/Server/MyLocalShop.Server.csproj", "MyLocalShop/Server/"]
COPY ["MyLocalShop.Services.MongoDb/MyLocalShop.Services.MongoDb.csproj", "MyLocalShop.Services.MongoDb/"]
COPY ["MyLocalShop.Server.Contracts/MyLocalShop.Server.Contracts.csproj", "MyLocalShop.Server.Contracts/"]
COPY ["MyLocalShop/Shared/MyLocalShop.Shared.csproj", "MyLocalShop/Shared/"]
COPY ["MyLocalShop/Client/MyLocalShop.Client.csproj", "MyLocalShop/Client/"]
RUN dotnet restore "MyLocalShop/Server/MyLocalShop.Server.csproj"
COPY . .
WORKDIR "/src/MyLocalShop/Server"
RUN dotnet build "MyLocalShop.Server.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyLocalShop.Server.csproj" -c Release -o /app/publish
FROM nginx:alpine AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY nginx.conf /etc/nginx/nginx.conf
ENTRYPOINT ["dotnet", "MyLocalShop.Server.dll"]
If I run the dotnet publish command to check the ouput directory within app/publish, I can see the file is actually there.
What am I missing?
This might happen when the file you want to COPY is also in the .dockerignore
Are you setting the context of the docker build to the directory that contains the nginx.conf and not a directory below that?
e.g.
nginx.conf
|-MyLocalShop
Bad: docker build -t something:latest -f ./DockerFile ./MyLocalShop
Good: docker build -t something:latest -f ./DockerFile .
Maybe you lack of nginx.conf file in your project.

Docker image creation - Net core 3.1 web API - Single solution with multiple projects

I have created web api in .net core 3.1 with docker file and include the DAL project. DAL project is common for all API. This is my project structure
--Solution
dockerfile
--DAL Project
This is my docker file format
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["Notification.csproj", ""]
COPY ["../DAL/DAL.csproj", "../DAL/"]
RUN dotnet restore "./Notification.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Notification.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Notification.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Notification.dll"]
While build the application VS Code I got below error
COPY failed: failed to resolve scoped path ..\DAL_DAL.csproj (): evalSymlinksInScope: \?\C:\ProgramData\Docker\tmp\DAL\DAL.csproj is not in \?\C:\ProgramData\Docker\tmp\docker-builder572417841. Possible cause is a forbidden path outside the build context
Any help would be appreciated.
this is my project structure;
And this is my Dockerfile in SampleApp.API;
Maybe it can be an example for you.

When using docker, I get DirectoryNotFoundException in PhysicalFileProvider in blazor

I created a blazor project using .netcore3.1.
I use PhysicalFileProvider in startup.cs.
I ran it with docker using config in docker.
The build was successful, but an exception occurred during execution.
The docker file is as follows:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["Web/Web.csproj", "Web/"]
RUN dotnet restore "Web/Web.csproj"
COPY . .
WORKDIR "/src/Web"
RUN dotnet build "Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Web.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Web.dll"]
This is a docker file automatically generated by Visual Studio.
Why can't the program find the directory?
Or, is there another way to find the directory?

"An assembly specified in the application dependencies manifest was not found" using docker

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.

how to build docker image file and push Vsts

How to address this error when I push my code on VSTS platform. I am using Visual Studio 2017.
I am new to the concept of Docker ,Azure (VSTS).
FROM microsoft/aspnetcore:1.1 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:1.1 AS build
WORKDIR /src
COPY ["aspnet-core-dotnet-core/aspnet-core-dotnet-core.csproj", "aspnet-core-dotnet-core/"]
RUN dotnet restore "aspnet-core-dotnet-core/aspnet-core-dotnet-core.csproj"
COPY . .
WORKDIR "/src/aspnet-core-dotnet-core"
RUN dotnet build "aspnet-core-dotnet-core.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "aspnet-core-dotnet-core.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "aspnet-core-dotnet-core.dll"]
Based on the DockerFile and error message, it failed to copy aspnet-core-dotnet-core/aspnet-core-dotnet-core.csproj file.
You need to specify DockerFile's parent folder path as the build context (Uncheck Use Default Build Context option and specify Build Context), then it can find the aspnet-core-dotnet-core/aspnet-core-dotnet-core.csproj file

Resources