dotnet restore not finding folder while running dockerfile - docker

This is a very simple API I've created with .NET 6 and ASP.NET Core that I'm trying to dockerize.
I'm always getting the same error while running docker build command.
this is my docker file:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore "MyAPI/MyAPI.csproj" //this is where the build stops
# Copy everything else and build
COPY .. ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
EXPOSE 80
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyAPI.dll"]
this is the error I'm getting :
=> ERROR [build-env 4/6] RUN dotnet restore 0.7s
------
> [build-env 4/6] RUN dotnet restore:
#11 0.676 MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
------
executor failed running [/bin/sh -c dotnet restore]: exit code: 1
I guess the problem is a path problem and I've tried my ways to pass in the path to my folder yet it kept giving me the same error.
If I try to run the dotnet restore command individually in the terminal, it works which tells me that the path is correct.
What could be the problem?

COPY *.csproj ./ doesn't find anything to copy, since your .csproj file is in the MyAPI/ directory.
Change the statement to
COPY MyAPI/MyAPI.csproj MyAPI/
Where you copy the rest of the files and publish the project should also be changed to
COPY . ./
RUN dotnet publish -c Release -o out MyAPI/MyAPI.csproj

Please check if the csproj file really exists in your PC file system on the path yourSolutionRootFolder/MyAPI/MyAPI.csproj
You may have a typo in the path? Upper/lower case typo?

Related

How to copy a single file using Dockerfile?

I am trying to build an ASP.NET Core project using Dockerfile. I need to use multiple package-sources to restore the project. so I defined the package-sources into a NuGet.config file.
Using docker, I want to copy the NuGet.config file to my working directory so that the dotnet restore command can use it to restore from.
Here is how my Dockerfile look like
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
LABEL stage=build-env
WORKDIR /app
# Copy and build
COPY ./src /app
COPY ./NuGet.config /app
RUN dotnet restore /app/Project.Web --configfile ./app/NuGet.config
RUN dotnet publish /app/Project.Web -c Release -o ./build/release --framework net6.0 --no-restore
Unfortunately, the above script errors on this line COPY ./NuGet.config /app
=> ERROR [build-env 4/6] COPY ./NuGet.config /app
failed to compute cache key: "/NuGet.config" not found: not found
How can I correctly copy the NuGet.config file over to my working directory?

can't build .net core API image in docker

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).

Did you mean to run dotnet SDK commands? Please install dotnet SDK from

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

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

Failed to restore nuget packages on Docker

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

Resources