Following the instructions on Docker's page I created a very simple Dockerfile that looks like this:
FROM microsoft/dotnet:sdk 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/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "dockertest.dll"]
This is literally a copy of the sample on Docker's site just changed the application name.
I used Visual Studio for Mac to create a very simple ASP.Net application (actually, just the default app with a tiny HTML file added in).
When I first ran docker build -t dockertest . the line with dotnet publish failed. I then ran the dotnet publish manually and got past that.
It now fails on the copy:
Step 9/10 : COPY --from=build-env /app/out .
COPY failed: stat /var/lib/docker/overlay2/1484306cebf1def83638270757e70a8cf874fb5a167f39e5bfaae92a47cc071c/merged/app/out: no such file or directory
What's going on?
So did you run dotnet publish inside the container or on the host machine? The Docker file is trying to run those commands inside the container, so running those on the host will not fix the issue.
Have you tried to right click your project and "Add Docker Support" to your app in VS for Mac? It should generate a docker file and then you can run/debug your app inside the container from within VS.
Related
I want to dockerize an existing .NET core 5 app and used the container tools to generate a Dockerfile. When I debug with Visual Studio 2022 it works but when I manually run it with the command docker build -t some-name . it generates the following error:
Step 7/21 : COPY ["MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj", "MechEng.MovableBridges.Api/"]
COPY failed: file not found in build context or excluded by .dockerignore: stat MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj: file does not exist
Why doesn't it find the file?
This is my Dockerfile that is inside the MechEng.MovableBridges.Api folder:
#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/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj", "MechEng.MovableBridges.Api/"]
COPY ["MechEng.MovableBridges.Infrastructure/MechEng.MovableBridges.Infrastructure.csproj", "MechEng.MovableBridges.Infrastructure/"]
COPY ["MechEng.MovableBridges.MathCad/MechEng.MovableBridges.Infrastructure.MathCad.csproj", "MechEng.MovableBridges.MathCad/"]
COPY ["MechEng.MovableBridges.Application/MechEng.MovableBridges.Application.csproj", "MechEng.MovableBridges.Application/"]
COPY ["MechEng.MovableBridges.Domain/MechEng.MovableBridges.Domain.csproj", "MechEng.MovableBridges.Domain/"]
RUN dotnet restore "MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj"
COPY . .
WORKDIR "/src/MechEng.MovableBridges.Api"
RUN dotnet build "MechEng.MovableBridges.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MechEng.MovableBridges.Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MechEng.MovableBridges.Api.dll"]
Your command fails, because the build context is wrong. Visual studio is using the solution root folder as build context, you are (probably) using the project's dockerfile's location. You can read more about the docker build command here.
Your command should look similar to this:
docker build -f "<path-to-your-dockerfile>" -t some-name "<!!!path-to-your-solution-folder!!!>"
You can see the exact command executed by visual studio in the output window, with "Container Tools" selected from the dropdown box.
In visual studio, you can move the docker file to one back folder.
For example, folder1/folder2/dockerfile move to folder1/dockerfile and build the image.
Docker build -t imageName .
Docker run -p 7778:80 imageName
I've created a simple .NET Core 3.0 web API. In the project directory, I have a Dockerfile that looks like this:
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build-env
WORKDIR /app
# copy over csproj and restore nuget packages
COPY DockerWebApiSandbox.csproj ./
RUN dotnet restore
# copy everything else over into the same directory as the last copy step
# and run the publish step to build and gather output
COPY . ./
RUN dotnet publish -c Release -o output
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DockerWebApiSandbox.dll"]
When running docker build -t dockerwebapisandbox . from the working directory of my project, I see a failure on step 9 of 10 (third COPY command):
COPY failed: stat
/var/lib/docker/overlay2/f6f3391827aef74f1dab5716635a9119ae250ae94a216bbc0bc7b47c4030d60a/merged/app/out:
no such file or directory
When searching what the community was saying about this error, I found a suggestion here. The suggestion mentions screening into the VM, but this command fails, given that the com.docker.driver.amd64-linux/ directory does not exist where it's being expected. The screen command suggested looks like this: $ screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty
Other than trying the suggestion from the link above, I've tried uninstalling Docker and Docker Desktop altogether, in addition to the "Factory Reset" option provided in Docker Desktop.
FWIW, I am running MacOS Mojave. TIA for anyone who has suggestions.
I have faced the same error in Node. I was resolve using
COPY . .
I am really curious to how to interpret and debug with the following error:-
C:\users\project>docker-compose build
Step 6/15 : COPY *.csproj ./
ERROR: Service 'invoiceservice' failed to build: COPY failed: no source files were specified
This is particular micro-service as i have few more such services.
docker file :-
FROM mcr.microsoft.com/dotnet/core/runtime:2.2 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /src
COPY *.csproj ./
RUN dotnet restore -s https://api.nuget.org/v3/index.json -s https://www.myget.org/F/autoweb/api/v3/index.json
COPY . .
WORKDIR /src/src/InvoiceManagement/InvoiceService
RUN dotnet publish -c Release -o out
FROM build AS publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/out .
ENTRYPOINT ["dotnet", "InvoiceService.dll"]
Interesting part is when I build this from Visual Studio IDE, its being built fine but it does not build on CLI.
docker compose file:-
invoiceservice:
image: ${DOCKER_REGISTRY-}invoiceservice
build:
context: .
dockerfile: src/InvoiceManagement/InvoiceService/Dockerfile
I don't understand why CLI could not find the source location and copy where as VS works fine. Any clue???
It's likely an issue with your Docker context. Commands in Dockerfiles are relative to where the docker/docker-compose CLI tool is run, not relative to the Dockerfile location.
In other words, if you run the command from the solution root, then your csproj file is actually under ./src/InvoiceManagement/InvoiceService. As a result, *.csproj finds no files, because there's no project files literally in your solution root.
I tried replicating your problem and I was able to copy all the files successfully (instead of .csproj, I used .txt). The problem occurred when there was no txt file to copy. My COPY command failed with exactly the same message as yours.
Now, why is VS able to build the image successfully? That is because VS build the project first! When the project's build procedure is completed, a .csproj file is generated and that gets copied to the image.
To confirm the results, ls your current directory (when the build fails from command line) and see if there is any .csproj file in that directory.
I want to run multiple instances of .net core API on windows server 2016 using windows docker container. I am able to create image and container successfully, but on invoking docker start the container are not running Up instead it exited with code (2147516566).
Here is my docker file content which is in published API directory
FROM mcr.microsoft.com/dotnet/core/runtime:2.2-nanoserver-sac2016
COPY / app/
ENTRYPOINT ["dotnet", "app/MyAPI.dll"]
I didn't spend long on it, but I didn't have good luck running binaries I built myself. The docker add in for visual studio always performs the build inside a container. I have adapted to this. Here is an example Dockerfile I have anonymized. Hopefully I didn't break anything:
# Base image for running final product
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-nanoserver-sac2016 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
# build asp.net application
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-nanoserver-sac2016 AS build
WORKDIR /src
COPY ["Test.Docker.Windows/Test.Docker.Windows.csproj", "Test.Docker.Windows/"]
RUN dotnet restore "Test.Docker.Windows/Test.Docker.Windows.csproj"
COPY . .
WORKDIR "/src/Test.Docker.Windows"
RUN dotnet build "Test.Docker.Windows.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Test.Docker.Windows.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
# startup.bat contains dotnet test.Docker.Windows.dll
CMD ./startup.bat
I just started a new solution with a .NET Core Project (2.1) using visual studio 15.8.8. It can run and debug it by setting the docker compose file as a startup project. It works!
Logically, I should be able to build the docker image with a simple commandline statement. However, it complains that the csproj cannot be found. This is strange. The file exist and as I told, I can run it from visual studio. I tried it from one directory up and the directory that has the dockerfile. Same problem.
How can I solve this? The only thing I want is simply build my image and then run it by just using docker commands.
Dockerfile
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["TryNewDocker2/TryNewDocker2.csproj", "TryNewDocker2/"]
RUN dotnet restore "TryNewDocker2/TryNewDocker2.csproj"
COPY . .
WORKDIR "/src/TryNewDocker2"
RUN dotnet build "TryNewDocker2.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "TryNewDocker2.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TryNewDocker2.dll"]
Het is the compose file:
version: '3.4'
services:
trynewdocker2:
image: ${DOCKER_REGISTRY}trynewdocker2
build:
context: .
dockerfile: TryNewDocker2/Dockerfile
Logically, I want "docker-compose up" to keep working when fixing this problem.
This is caused by the wrong root folder for the file path in dockerfile.
For launching from Docker, its root folder is C:\Users\...\repos\TryNewDocker2, but while running from command, its root fodler is C:\Users\...\repos\TryNewDocker2\TryNewDocker2, so the path for TryNewDocker2.csproj has changed from TryNewDocker2/TryNewDocker2.csproj to TryNewDocker2.csproj
Try dockerfile below:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 59162
EXPOSE 44342
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["TryNewDocker2.csproj", "TryNewDocker2/"]
RUN dotnet restore "TryNewDocker2/TryNewDocker2.csproj"
COPY . ./TryNewDocker2/
WORKDIR "/src/TryNewDocker2"
RUN dotnet build "TryNewDocker2.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "TryNewDocker2.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TryNewDocker2.dll"]
Update
For working in both Docker and command, do not change your dockerfile, and from path below to run your command with specifying the dockerfile path.
C:\Users\...\repos\TryNewDocker2>docker build -t gogo -f TryNewDocker2/Dockerfile .
For those of you who end up here years later like I did, I'll share my experience.
My problem was caused by the auto-generated Dockerfile that came from Visual Studio's "add > Docker Support..." was on the same level as my .csproj file.
The specific line causing me trouble was COPY ["MyApp/MyApp.csproj", "MyApp/"] which should have been just COPY ["MyApp.csproj", "MyApp/"]. Removing the extra MyApp/ in front of the .csproj got the build working fine.
Special thanks to Edward in the answer above for pointing me in the right direction.