Docker Copy failed with ASP.net Core - c#

I am trying to run my ASP.net Core application with docker, but for some reasons each time I try with different approach with the Dockerfile I fail by copying the directory.
What am I doing wrong? I tried the step by step guide in Microsoft web using my own proj but with unsuccess.
The cmd error I recieve:
C:\dev\Dmail\Dmail.API>docker build -t aspnetapp .
Sending build context to Docker daemon 21.79MB
Step 1/18 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
---> 47da1e9634dc
Step 2/18 : WORKDIR /app
---> Using cache
---> bf490aa67962
Step 3/18 : EXPOSE 80
---> Using cache
---> d172deff350a
Step 4/18 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
---> 4a651b73be3d
Step 5/18 : WORKDIR /src
---> Using cache
---> c607b7f04e23
Step 6/18 : COPY ["Dmail.API/Dmail.API.csproj", "Dmail.API/"]
COPY failed: stat /var/lib/docker/tmp/docker-builder531905822/Dmail.API/Dmail.API.csproj: no such file or directory
C:\dev\Dmail\Dmail.API>
Here is my DockerFile:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Dmail.API/Dmail.API.csproj", "Dmail.API/"]
COPY ["Dmail.Core/Dmail.Core.csproj", "Dmail.Core/"]
COPY ["Dmail.Data/Dmail.Data.csproj", "Dmail.Data/"]
RUN dotnet restore "Dmail.API/Dmail.API.csproj"
COPY . .
WORKDIR "/src/Dmail.API"
RUN dotnet build "Dmail.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Dmail.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Dmail.API.dll"]

So after long time of searching the web for a solution I managed to solve my problem.
First of all the Dockerfile was generated automatically by Visual Studio by pressing add onto the ASP.net project and then Docker Support.
The Visual Studio locate the file inside the project. What I did in order to solve the problem was to move the file as it is to the solution folder which is for me Dmail. For example my files were like this:
Dmail(solution)
Dmail.API(proj)
Dmail.Core(proj)
Dmail.Data(proj)

Related

Exception on .setting file Save() function on docker Linux: ClientConfigurationHost::OpenStreamForWrite

im working on a net7.0 ASP.NET Core Wen API project with linux docker build.
In my application i need to save some settings on a .setting file. Thats working well when i run my application on IIS Express on Windows.
Here is a simpel code example:
ApplicationSettings.Default.Test = "Test"
ApplicationSettings.Default.Save();
When i running this code on Docker build:
I get this exception on the .Save() functon:
"Failed to save settings: An error occurred loading a configuration file: An unexpected error occurred in 'ClientConfigurationHost::OpenStreamForWrite '' '''."
This docker file was created by visual studio.
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["ShippApiCollection/ShippApiCollection.csproj", "ShippApiCollection/"]
RUN dotnet restore "ShippApiCollection/ShippApiCollection.csproj"
COPY . .
WORKDIR "/src/ShippApiCollection"
RUN dotnet build "ShippApiCollection.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ShippApiCollection.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ShippApiCollection.dll"]
Anybody had the same issue and can help me here?

Azure Devops Pipeline - file not found in build context

Getting an error in the Azure DevOps Pipeline:
Step 7/17 : COPY ["demo6/demo6.csproj", "demo6/"]
COPY failed: file not found in build context or excluded by .dockerignore: stat demo6/demo6.csproj: file does not exist
NOTES-run the pipeline in diagnostics mode and the .sln file exists
##[debug]cwd=/home/vsts/work/1/s
##[debug] /home/vsts/work/1/s/demo6.sln (file)
##[debug] /home/vsts/work/1/s/demo6/demo6.csproj (file)
I have a multi-project Asp.Net Core solution with the folder structure and projects as follows:
demo6
|--demo6/demo6.csproj
|--demo6.api/demo6.api.csproj
The app is demo6 which references demo6.api which is a class library.
This is in GitHub in a repository demo6. I modified the autogenerated Dockerfile to add the extra demo6/ to see if that works but no.
Appreciate any help.
Dockerfile below:
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 ["demo6/demo6.csproj", "demo6/"]
RUN dotnet restore "demo6/demo6.csproj"
COPY . .
WORKDIR "/src/demo6"
RUN dotnet build "demo6.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "demo6.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "demo6.dll"]
Also tried this:
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 ["demo6/demo6/demo6.csproj", "ddemo6/"]
RUN dotnet restore "ddemo6/demo6.csproj"
COPY . .
WORKDIR "/src/ddemo6"
RUN dotnet build "demo6.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "demo6.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "demo6.dll"]
Got this error:
Step 7/17 : COPY ["demo6/demo6/demo6.csproj", "ddemo6/"]
COPY failed: file not found in build context or excluded by .dockerignore: stat demo6/demo6/demo6.csproj: file does not exist
##[error]COPY failed: file not found in build context or excluded by .dockerignore: stat demo6/demo6/demo6.csproj: file does not exist
here's the repo structure
The reason of file not found error may be the relative path of the Dockerfile and the csproj are different. Try to set the Dockerfile in the root of the solution should solve your error.
Also you need to add a buildContext with the csproj folder.
buildContext: Path to the build context
Also, here is a case you can refer to.
One more possible cause of COPY failed: no source files were specified is .dockerignore file present in your workspace
Look for .dockerignore file because of docker’s CLI (command line interface) it sends context to the docker daemon to load the .dockerignore file.
If the .dockerignore file exist in the context than it might be possible there is exclude entry to ignore the build directory
# ignore or exclude build directory
*/build*
*/*/build*

docker-compose build copy failed

I'm trying to upload my project to a Digital Ocean droplet using docker i'm stuck in this stage, where COPY fails whenever i run docker-compose build.
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["iCoose.API/iChoose.API.csproj", "iCoose.API/"]
COPY ["iChoose.Business.Entities/iChoose.Business.Entities.csproj", "iChoose.Business.Entities/"]
COPY ["iChoose.Business.Services/iChoose.Business.Services.csproj", "iChoose.Business.Services/"]
COPY ["iChoose.Common.Core/iChoose.Common.Core.csproj", "iChoose.Common.Core/"]
COPY ["iChoose.DataAccess.Data/iChoose.DataAccess.Data.csproj", "iChoose.DataAccess.Data/"]
RUN dotnet restore "iCoose.API/iChoose.API.csproj"
COPY . .
WORKDIR "/src/iCoose.API"
RUN dotnet build "iChoose.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "iChoose.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "iChoose.API.dll"]
docker-compose.yml:
#docker-compose
version: '2'
services:
app:
build:
context: ./
dockerfile: Dockerfile
https-portal:
image: steveltn/https-portal:1
ports:
- '80:80'
- '443:443'
links:
- app
restart: always
environment:
DOMAINS: 'domain.com -> http://app:5000'
STAGE: 'production'
When I run the 'docker-compose build' command in the servers terminal it starts building until it gets to the first COPY command, where I get:
ERROR: Service 'app' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder987069796/iCoose.API/iChoose.API.csproj: no such file or directory.
I checked the files and there is no directory in that path after the 'tmp' folder, which is empty.
The Dockerfile and docker-compose.yaml files should be right next to sln and csproj file.
When you create a new project in Visual Studio and leave put project file inside the project folder this issue might occure.
Make sure you have sln and csproj in the same folder.
Also, the Dockerfile and 'docker-compose.yaml` in the same folder too.
Deeper answer:
Dockerfile contains instructions for the docker engine on how to create a docker image for your application.
Each line is a layer of docker image.
FROM
COPY
RUN
CMD
All these commands will create a new docker image, which are cache by the engine for next run.
Because these images are Linux based and linux uses cgroup and namespaces to create isolated environment which is the feature on which docker is built. This isolated environment has a special folder which is appearing in the error message.

Dockerized application does not find filesystem image

I have followed these steps:
1) Create a new ASP.NET Core Web application (MVC) without Docker support.
2) Create two new folders (one inside the other) inside the solution called Application\Image (Application is at the same level as the Controllers folder).
3) Add a file called TestImage.png into the Image folder.
4) Access the file successfully using the following code:
byte[] image = System.IO.File.ReadAllBytes("Application\\Image\\TestImage.png");
Expected behaviour up to now; here is the problematic bit:
5) Right click on the solution and select: Add Container Orchestration Support/Docker Compose/Linux (target OS)
6) Run the project (docker-compose is the startup project). I get an error saying: System.IO.IOException: 'Invalid argument'
I have done a lot of Googling and it tells me I have to configure the Dockerfile to copy the image to the container. The Dockerfile currently looks like this:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY WebApplication1/WebApplication1.csproj WebApplication1/
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 .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]
I have tried adding this line (below this line: COPY WebApplication1/WebApplication1.csproj WebApplication1/):
COPY WebApplication1/Application/Image/TestImage.png WebApplication1/Application/Image/
However, I see the same error. How can I get the app to read the image now I am using Docker?

New Asp.Net core project not getting build with docker command

I created new Asp.net core project in VS2017, and selected docker support for linux. the docker file looks like below
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["WebApp2/WebApp2.csproj", "WebApp2/"]
RUN dotnet restore "WebApp2/WebApp2.csproj"
COPY . .
WORKDIR "/src/WebApp2"
RUN dotnet build "WebApp2.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "WebApp2.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApp2.dll"]
When i run the below docker build command, I'm getting an error.
docker buid -t mylogin/dotnetcoreapp1 .
Error Message
Sending build context to Docker daemon 4.125MB
Step 1/16 : FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
---> 651a14e0b96b
Step 2/16 : WORKDIR /app
---> Running in 929f4fdb28f2
Removing intermediate container 929f4fdb28f2
---> 54cdd8399e5b
Step 3/16 : EXPOSE 80
---> Running in 844cfb6d1638
Removing intermediate container 844cfb6d1638
---> b15703d4c881
Step 4/16 : FROM microsoft/dotnet:2.1-sdk AS build
---> 1f3b89fbfa01
Step 5/16 : WORKDIR /src
---> Running in 07375b9916a3
Removing intermediate container 07375b9916a3
---> dc4227c9d5ec
Step 6/16 : COPY ["WebApp2.csproj", ""]
---> 1cd0239157a4
Step 7/16 : RUN dotnet restore "/WebApp2.csproj"
---> Running in 73fad81058fd
MSBUILD : error MSB1001: Unknown switch.
Switch: /WebApp2.csproj
For switch syntax, type "MSBuild /help"
The command 'cmd /S /C dotnet restore "/WebApp2.csproj"' returned a non-zero code: 1
I faced the same issue, Just move the dockerfile one level up
for example your current csproj path is
c:\projects\MySolutionFolder\MyProj\MyProj.csproj (Where docker file is here as well)
Move the docker file to be in "c:\projects\MySolutionFolder\"
I hop it helps you.

Categories