How to copy a single file using Dockerfile? - docker

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?

Related

dotnet restore not finding folder while running dockerfile

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?

How to copy the contents from the previous directory in Dockerfile

Let's take an example of dotnet application code
I have an application code present at
D:\eshop\DotneteShopOnWeb\src\Web\Application.sln
and I have multiple projects in the "Application.sln" like Web, API, Test
every project is having its own Dockerfile like
D:\eshop\DotneteShopOnWeb\src\Web>Dockerfile
The Dockerfile is as below
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
WORKDIR /app
COPY *.sln .
COPY . .
WORKDIR /app/src/Web
RUN dotnet restore
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS runtime
WORKDIR /app
COPY --from=build /app/src/Web/out ./
ENV ASPNETCORE_ENVIRONMENT Development
ENTRYPOINT ["dotnet", "Web.dll"]
I am executing the docker build command Dockerfile location.
I have also tried docker build -t dotnetcore-eshop-mvc-manual -f src/Web/Dockerfile . from D:\eshop\DotneteShopOnWeb>
I am getting errors in RUN dotnet restore as the sln file is not getting copied into the work directory.
Please let me know what modification I need in COPY to copy content from the previous directory.
you are changing the working directory before restoring. I mean that your .sln files are in the path /app but your restoring is executed inside /app/src/Web location. you should do something like this to copy .sln files.
COPY *.sln .
COPY . .
WORKDIR /app/src/Web
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS runtime
WORKDIR /app
COPY --from=build /app/src/Web/out ./
ENV ASPNETCORE_ENVIRONMENT Development
ENTRYPOINT ["dotnet", "Web.dll"]

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

"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 point correct path in the Dockerfile

I have .sln file and Dockerfile at a different location on my local hard drive.
Till now I work on a source code where .sln file and Dockerfile are present at the same location.
I am facing difficulty to configure it correctly.
I have .sln file at the location CompanyCarsDocker\CompanyCarsDocker\CompanyCarsDocker.sln
And My Dockerfile is present inside CompanyCarsDocker\CompanyCarsDocker\CarApi\Dockerfile
-
Below is my Dockerfile
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 54411
FROM microsoft/aspnetcore-build:2.0 AS builder
WORKDIR /src
COPY . ./*.sln ./
ADD ./site /inetpub/wwwroot
COPY ./CarApi.csproj CarApi/
RUN dotnet restore
COPY . .
WORKDIR /src/CarApi
RUN dotnet build -c Release -o /app
FROM builder AS publish
RUN dotnet publish -c Release -o /app
FROM base AS production
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "CarApi.dll"]
I have added ADD ./site /inetpub/wwwroot for testing purpose. I am getting
ADD failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-builder782713904\site: The system cannot find the file specified.
Same error I am also Getting at COPY ./CarApi.csproj CarApi/
Please let me know if I configured something wrong in the Dockerfile.
You can use the -f flag of docker build to specify the path to the Dockerfile
Make sure you’re current working directory is the one which has the sln file and rest of the project. Run the command docker build -f <path to Dockerfile> .
your .sln file is two directories up so you should use
COPY ../../*.sln ./
instead of
COPY . ./*.sln ./

Resources