dotnet core 2.2 app DockerFile COPY Fails /overlay2/ - docker

My dotnet core 2.2 app have this structure (which is pretty standard):
- .sln
- DockerFile
- MyApp/
|- *.csproj
My DockerFile is this:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY ./MyApp/*.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyApp.dll"]
I always get this error:
Step 9/10 : COPY --from=build-env /app/out .
ERROR: Service 'myapp' failed to build: COPY failed: stat /var/lib/docker/overlay2/ed30d6e8a88b5d65bb14a5e972428135a78701c860f510d4dcc3e5c4947916d3/merged/app/out: no such file or directory
I looked online and could only find suggestions about resetting the data of Docker, which I tried without any kind of success.
I'm building using the command docker build . from the DockerFile repository.
Is there something missing from my DockerFile? Do you have any clue to help me figure this out?

I have similar dockerfile. but my output is /out
and also copy from /out
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY ./MyApp/*.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o /out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /out .
ENTRYPOINT ["dotnet", "MyApp.dll"]

Related

How Dockerfile find references

I would like to understand how both dockerfiles works. Specially when restoring project references.
The original dockerfile only has main project in copy, even this project has some dependecies. How are this dependencies restored?
Original
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY ["MyApplication.Api/MyApplication.Api.csproj", "MyApplication.Api/"]
RUN dotnet restore "MyApplication.Api/MyApplication.Api.csproj"
# Copy everything else and build
COPY . .
WORKDIR "/app/MyApplication.Api"
RUN dotnet build "MyApplication.Api.csproj" -c Debug -o /app/build
RUN dotnet publish -c Debug -o /app/out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyApplication.Api.dll"]
And this is the dockerfile generated by VisualStudio2022 when docker support is added. In this case it specifies all project references which makes sense to me.
VS2022
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyApplication.Api/MyApplication.Api.csproj", "MyApplication.Api/"]
COPY ["MyApplication.Common.Resources/MyApplication.Common.Resources.csproj", "MyApplication.Common.Resources/"]
COPY ["MyApplication/MyApplication.Common.Services/MyApplication.Common.Services.csproj", "MyApplication/MyApplication.Common.Services/"]
COPY ["MyApplication/MyApplication.Common.Domain/MyApplication.Common.Domain.csproj", "MyApplication/MyApplication.Common.Domain/"]
COPY ["MyApplication/MyApplication.Common.DataAccess/MyApplication.Common.DataAccess.csproj", "MyApplication/MyApplication.Common.DataAccess/"]
RUN dotnet restore "MyApplication.Api/MyApplication.Api.csproj"
COPY . .
WORKDIR "/src/MyApplication.Api"
RUN dotnet build "MyApplication.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyApplication.Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApplication.Api.dll"]
So the exact question would be, how the first option solve references since those are not specified??
The thing is that some of my projects failed on AzureDevops pipelines since it does not find the references, but others work well with the "original" version and this does not make sense at all for me.

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

.Net core docker build with multiple projects

Net core project and I have many webapi, business layer and some other layers here. Below is the folder stucture.
Main Solution
businessLayer
data layer
Web API layer
3.1 MyDockerFile
Below is my docker file.
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
COPY ["Enrichment.WebApi/Enrichment.WebApi.csproj", "Enrichment.WebApi/"]
COPY ["Enrichment.Facade/Enrichment.Facade.csproj", "Enrichment.Facade/"]
COPY ["Enrichment.Saga/Enrichment.Saga.csproj", "Enrichment.Saga/"]
COPY ["Enrichment.BusinessModel/Enrichment.BusinessModel.csproj", "Enrichment.BusinessModel/"]
RUN dotnet restore "Enrichment.WebApi/Enrichment.WebApi.csproj"
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Enrichment.WebApi.dll"]
When I run docker build -t enrichment.webapi . I get below output
Sending build context to Docker daemon 230.8MB
Step 1/16 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
---> 9a88f73dec65
Step 2/16 : WORKDIR /app
---> Using cache
---> 9b50f1fc6721
Step 3/16 : COPY ["Enrichment.WebApi/Enrichment.WebApi.csproj", "Enrichment.WebApi/"]
COPY failed: stat /var/lib/docker/tmp/docker-builder437963979/Enrichment.WebApi/Enrichment.WebApi.csproj: no such file or directory
I am not able to understand why I am getting above error. Path looks like incorrect but I am not able to understand what would be the right path. can someone help me to understand any issue here. Any help would be appreciated. Thank you.
Step 9/16 : COPY *.csproj ./ COPY failed: no source files were specified
This is because of .dockerignore file that ignores the csproj. Notice the content of .dockerignore below and Anything other than obj/Docker/publish/* and obj/Docker/empty is ignored.
*
!obj/Docker/publish/*
!obj/Docker/empty/
Incude this in your dockerfile
# build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out ./

asp.net core 2.0 docker file issue in windows

I have made a asp.net core 2.0 web API.
Now I want to create a docker image for this API.
Here is my docker file content -
FROM microsoft/aspnetcore:2.0-nanoserver-1803 AS build-env
WORKDIR /app
FROM microsoft/aspnetcore-build:2.0-nanoserver-1803 AS base
WORKDIR /src
# 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 base AS final
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Microservice_Orders.dll"]
I am running the following command to create an image
docker build -t ms_orders .
But I am getting following error on execution.
******Step 11/12 : COPY --from=build-env /app/out . COPY failed: CreateFile \?\Volume{a3251a00-f8f4-4510-8db2-f495f33ce178}\app\out:
The system cannot find the file specified.******
I found a few things to be a bit strange in that dockerfile.
It looks like you have base and build-env mixed up, i.e. you're building into base, but then trying to copy what you built from build-env.
Also the path from dotnet publish doesn't match the path in COPY --from=build-env.
/src/out (I think) vs /app/out
I would try to change it to below. I have copied the dockerfile and commented where I've made changes.
FROM microsoft/aspnetcore:2.0-nanoserver-1803 AS base # name changed since this is your base
WORKDIR /app
EXPOSE 80 # You'll probably also need to expose the app on some port(s)
EXPOSE 443 # depending on if you want https or not, you can remove one of these lines
FROM microsoft/aspnetcore-build:2.0-nanoserver-1803 AS build-env # name changed since this is where you build
WORKDIR /src
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o /app/out # output path changed!
# Build runtime image
FROM base AS final
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Microservice_Orders.dll"]
Disclaimer: I haven't actually tried to build it, so there could be a few more issues.

Docker on Windows does not support COPY --from?

I am trying to make use of this Docker construct:
Optionally a name can be given to a new build stage by adding AS name
to the FROM instruction. The name can be used in subsequent FROM and
COPY --from= instructions to refer to the image built in
this stage.
But I get this message:
COPY failed: stat /var/lib/docker/tmp/docker-builder869050639/-–from=build-env:
no such file or directory
Is copying files from a previous stage supported on Docker for Windows 17.09.0-ce-win33 (13620)? This is the Dockerfile:
FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app
COPY ./ ./
RUN dotnet build ProviderApi.csproj
RUN dotnet publish ProviderApi.csproj -c Release -r linux-x64 -o out
FROM microsoft/dotnet:2.0-runtime-deps
WORKDIR /app
COPY -–from=build-env /app/out/ ./
EXPOSE 80
ENTRYPOINT ["./ProviderApi"]
The problem was that the destination directory was not present in the docker image of the final build stage. Here is the working Dockerfile for future reference.
FROM microsoft/aspnetcore-build:2.0.3-stretch AS build-env
WORKDIR /app
COPY . ./
RUN dotnet build ProviderApi.csproj
RUN dotnet publish ProviderApi.csproj -c Release -r linux-x64 -o out
FROM microsoft/aspnetcore:2.0.3-stretch
RUN mkdir /app
COPY --from=build-env /app/out /app
ENV ASPNETCORE_URLS=http://+:5001
EXPOSE 5001
ENTRYPOINT ["/app/ProviderApi"]
have you tried with lowercase "as"? (seems like a long-shot, I see examples of both around)
FROM microsoft/dotnet:2.0-sdk as build-env
or the more basic
FROM microsoft/dotnet:2.0-sdk
COPY -–from=0 /app/out/ ./

Resources