I am trying to build a docker image for an ASP.NET Core 3.1 application and I fail to restore the packages. My solution has multiple projects and after copying all the project files, I am trying to create a layer which contains restored dependencies only.
The docker file
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["ExampleApi/ExampleApi.csproj", "ExampleApi/ExampleApi/"]
COPY ["Common.Web/Common.Web.csproj", "ExampleApi/Common.Web/"]
COPY ["Example.Data/Example.Data.csproj", "ExampleApi/Example.Data/"]
COPY ["Example.Services/Example.Services.csproj", "ExampleApi/Example.Services/"]
RUN mkdir /tmp/build/
COPY . /tmp/build
RUN find /tmp/build -name *.csproj
RUN dotnet restore "ExampleApi/ExampleApi.csproj" --verbosity detailed
COPY . .
WORKDIR "/src/ExampleApi/ExampleApi"
RUN dotnet build "ExampleApi.csproj" -c Release -o /app --no-restore
FROM build AS publish
RUN dotnet publish "ExampleApi.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ExampleApi.dll"]
Docker build
I am running without cache to be able to output all the csproj files within the container for each run (otherwise it gets cached and the files are not longer displayed).
docker build --no-cache -f ExampleApi/Dockerfile .
Sending build context to Docker daemon 116.4MB
Step 1/22 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
---> a843e0fbe833
Step 2/22 : WORKDIR /app
---> Running in e33e9c821c0b
Removing intermediate container e33e9c821c0b
---> eec973a226e0
Step 3/22 : EXPOSE 80
---> Running in 8e813a3a8b85
Removing intermediate container 8e813a3a8b85
---> c8d95a3a1de4
Step 4/22 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
---> cef7866e800b
Step 5/22 : WORKDIR /src
---> Running in f56bf9402ed1
Removing intermediate container f56bf9402ed1
---> 55694332db0e
Step 6/22 : COPY ["ExampleApi/ExampleApi.csproj", "ExampleApi/ExampleApi/"]
---> 4beb2e169ec7
Step 7/22 : COPY ["Common.Web/Common.Web.csproj", "ExampleApi/Common.Web/"]
---> ed7a9c81e8f7
Step 8/22 : COPY ["Example.Data/Example.Data.csproj", "ExampleApi/Example.Data/"]
---> aaca52c111b4
Step 9/22 : COPY ["Example.Services/Example.Services.csproj", "ExampleApi/Example.Services/"]
---> efb15c17a096
Step 10/22 : RUN mkdir /tmp/build/
---> Running in 7da9eae324ca
Removing intermediate container 7da9eae324ca
---> b06126b19a7f
Step 11/22 : COPY . /tmp/build
---> 04598eaab98c
Step 12/22 : RUN find /tmp/build -name *.csproj
---> Running in 2fd26dba963d
/tmp/build/ExampleApi/ExampleApi.csproj
/tmp/build/Example.Data/Example.Data.csproj
/tmp/build/Common.Web/Common.Web.csproj
/tmp/build/Example.Sql/Example.!Sql.csproj
/tmp/build/Example.Services/Example.Services.csproj
/tmp/build/Example.Services.Tests/Example.Services.Tests.csproj
/tmp/build/Example.IdentityServer.Infrastructure/Example.IdentityServer.Infrastructure.csproj
/tmp/build/Example.IdentityServer/Example.IdentityServer.csproj
/tmp/build/Example.Data.Tests/Example.Data.Tests.csproj
/tmp/build/ExampleIntegrationApi/ExampleIntegrationApi.csproj
Removing intermediate container 2fd26dba963d
---> 78eadf796305
Step 13/22 : RUN dotnet restore "ExampleApi/ExampleApi.csproj" --verbosity detailed
---> Running in 5dc9b5bbd04e
MSBUILD : error MSB1009: Project file does not exist.
Switch: ExampleApi/ExampleApi.csproj
The command '/bin/sh -c dotnet restore "ExampleApi/ExampleApi.csproj" --verbosity detailed' returned a non-zero code: 1
find indicates that ExampleApi/ExampleApi.csproj is there, yet the dotnet restore complains about not finding it.
I have a vanilla .NET Core 2 web app that I setup in JetBrains Rider and I immediately started working on a Docker environment for it. I followed this guide to get started:
https://docs.docker.com/engine/examples/dotnetcore/
I altered it slightly to come up with this:
FROM microsoft/dotnet:latest AS packager
RUN mkdir -p /opt/build
WORKDIR /opt/build
# Copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore
# Copy everything else and build
COPY . .
RUN dotnet publish -c Release -o bin
# --
# Build runtime image
FROM microsoft/dotnet:runtime AS runtime
RUN mkdir -p /opt/app
WORKDIR /opt/app
COPY --from=packager /opt/build/bin/. .
ENTRYPOINT ["dotnet", "/opt/app/aspnetapp.dll"]
The image builds and when I go to run the container I get the following output:
dan#mycomputer ~/Desktop/coreapi (master)
$ docker build -t myapp .
Sending build context to Docker daemon 25.09kB
Step 1/12 : FROM microsoft/dotnet:latest AS packager
---> e1a56dca783e
Step 2/12 : RUN mkdir -p /opt/build
---> Using cache
---> 95f9c936d0d1
Step 3/12 : WORKDIR /opt/build
---> Using cache
---> 64f26c356fd7
Step 4/12 : COPY *.csproj .
---> Using cache
---> 38a2fb7ca6bb
Step 5/12 : RUN dotnet restore
---> Using cache
---> 70dbc44d98ae
Step 6/12 : COPY . .
---> Using cache
---> b1019d53a861
Step 7/12 : RUN dotnet publish -c Release -o bin
---> Using cache
---> 8e112606633a
Step 8/12 : FROM microsoft/dotnet:runtime AS runtime
---> cc240a7fd027
Step 9/12 : RUN mkdir -p /opt/app
---> Using cache
---> 954f494febc4
Step 10/12 : WORKDIR /opt/app
---> Using cache
---> b74be941e7dc
Step 11/12 : COPY --from=packager /opt/build/bin/. .
---> Using cache
---> 4c229192d99b
Step 12/12 : ENTRYPOINT ["dotnet", "/opt/app/aspnetapp.dll"]
---> Using cache
---> fb6ef4015fba
Successfully built fb6ef4015fba
Successfully tagged myapp:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
dan#mycomputer ~/Desktop/coreapi (master)
$ docker run -p 5001:5001 myapp:latest
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
The app doesn't run and I get a message that says I need to install the SDK. What's up with that? Shouldn't the runtime Docker image have everything neede to run the app?
I was able to find the solution by modifying the ENTRYPOINT to run tail -f /dev/null. From there, I entered the container and saw that the name of the binary adjusts based on your project name which the Docker documentation didn't make clear to me.
I also updated my base images and this solved my problem. Here is my latest Dockerfile below:
FROM microsoft/dotnet:2.1-sdk AS packager
RUN mkdir -p /opt/build
WORKDIR /opt/build
# Copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore
# Copy everything else and build
COPY . .
RUN dotnet publish -c Release -o bin
# --
# Build runtime image
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
RUN mkdir -p /opt/app
WORKDIR /opt/app
COPY --from=packager /opt/build/bin/. .
EXPOSE 80
ENTRYPOINT ["dotnet", "/opt/app/coreapi.dll"]
While trying to deploy my ASP.Net Core 2.1 app using this article I'm stuck on the problem.
Sending build context to Docker daemon 25.18MB
Step 1/10 : FROM microsoft/dotnet:2.1-aspnetcore-runtime AS build-env
---> 625b44243fbe
Step 2/10 : WORKDIR /app
---> Using cache
---> a8c5d2ab76c0
Step 3/10 : COPY *.csproj ./
---> Using cache
---> c2c11e2b4699
Step 4/10 : RUN dotnet restore
---> Running in 4ae02c24aabc
[91mDid you mean to run dotnet SDK commands? Please install dotnet SDK from:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
docker : The command '/bin/sh -c dotnet restore' returned a non-zero code: 145
At D:\Projects\Ozon\solutionName\projectname\deploy.ps1:3 char:1
+ docker build -t projectname .
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The command '/b...-zero code: 145:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
There is the deploy script I'm running to dockerize my app.
docker build -t projectname .
docker run -d -p 8080:80 --name api projectname
There is my DOCKERFILE
FROM microsoft/dotnet:2.1-aspnetcore-runtime 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:2.1-aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "projectname.dll"]
What am I doing wrong? Any suggestions would be appreciated.
you're using microsoft/dotnet:2.1-aspnetcore-runtime instead microsoft/dotnet:2.1-sdk. change the base image.
Trying to follow the tutorial found here, but running into problems.
I run the following command from my project dir:
docker build -t my.solution .
I get the following:
Sending build context to Docker daemon 111.6kB
Step 1/17 : FROM microsoft/aspnetcore:2.0-nanoserver-1709 AS base
---> ccfb41c8f5b5
Step 2/17 : WORKDIR /app
---> Using cache
---> e29a68e16001
Step 3/17 : EXPOSE 80
---> Using cache
---> 976388139964
Step 4/17 : FROM microsoft/aspnetcore-build:2.0-nanoserver-1709 AS build
---> d7ab4e860769
Step 5/17 : WORKDIR /src
---> Using cache
---> 4ab01220723e
Step 6/17 : COPY my.solution.sln ./
COPY failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-builder564035917\my.solution.sln: The system cannot find the file specified.
I don't know why it's trying to find the file in the location it's looking for it. Can anyone help me? Is there a config setting I need to make? My Docker file looks like this:
FROM microsoft/aspnetcore:2.0-nanoserver-1709 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0-nanoserver-1709 AS build
WORKDIR /src
COPY my.solution.sln ./
COPY my.solution/my.solution.csproj my.solution/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/my.solution
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "my.solution.dll"]
UPDATE
Per #AlexGera's answer, I tried changing my docker file to:
FROM microsoft/aspnetcore:2.0-nanoserver-1709 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0-nanoserver-1709 AS build
WORKDIR /src
VOLUME C:/tmp
COPY my.solution.sln c:/tmp/
COPY my.solution/my.solution.csproj my.solution/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/my.solution
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "my.solution.dll"]
but the error message doesn't change significantly:
docker build -t my.solution .
Sending build context to Docker daemon 111.6kB
Step 1/18 : FROM microsoft/aspnetcore:2.0-nanoserver-1709 AS base
---> ccfb41c8f5b5
Step 2/18 : WORKDIR /app
---> Using cache
---> e29a68e16001
Step 3/18 : EXPOSE 80
---> Using cache
---> 976388139964
Step 4/18 : FROM microsoft/aspnetcore-build:2.0-nanoserver-1709 AS build
---> d7ab4e860769
Step 5/18 : WORKDIR /src
Removing intermediate container 31e30e2346aa
---> 61c7df20f3c4
Step 6/18 : VOLUME C:/tmp
---> Running in fada6c728151
Removing intermediate container fada6c728151
---> 7a650440cc1f
Step 7/18 : COPY my.solution.sln c:/tmp/
COPY failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-builder832533802\my.solution.sln: The system cannot find the file specified.
Seems to me the standard Dockerfile that comes with a new solution is bugged :/
I moved the Dockerfile up to the solution folder, from PowerShell:
mv Dockerfile ../Dockerfile
Running the docker build command from there did the trick for me...
The COPY command will copy the file from the build context, or a previous build stage if you specify the stage. With your build command:
docker build -t my.solution .
The context is . which is first sent to the docker engine which places that in a temporary location just for the build.
Therefore with your copy command:
Step 6/17 : COPY my.solution.sln ./
The file my.solution.sln needs to exist in the folder where the build command was run.
For the destination, the file will be copied to the working directory inside the container, or /src in your example.
This was probably caused by the .dockerignore file next to you DockerFile, ignoring everything but /obj/*.
Once you copied it to another folder you didn't copy the .dockerignore file, so nothing was excluded and it worked.
Before copying add a volume in your image to copy where to.
Something like this:
VOLUME C:/Users/mysolution
COPY my.solution.sln C:/Users/mysolution
Try not to use dots for directory names.
I found that same situation in a VS2017 Solution where the build is started by docker compose and yml files one directory above the project.
If you want to build by docker build with the docker file directly, you need to move the docker file one level above the context
Use the \\
FROM microsoft\\aspnetcore-build:2.0-nanoserver-1709 AS build
see the below example
FROM microsoft/aspnetcore:2
WORKDIR /app
EXPOSE 80
COPY bin\\Debug\\netcoreapp2.0 .
ENTRYPOINT ["dotnet","DOCKER-API.dll"]
I move my Dockerfile to the root folder (where exists .sln and .dockerignore files) and my problem was resolved
Perfect suits for who uses visual studio :
run this command :
docker build -t . your_application_name/Dockerfile
Dockerfile Copy path :
["your_application_name/your_application_name.csproj","your_application_name/"]
we run the build command docker build command it is unable to find the dockerfile in the project path location.
Visual Studio creates the Docker file at Project level, however, the Dockerfile is tailored to be run from an upper level (Solution level).
The easiest way to fix this problem is to go one level up (Solution folder), and specify the project folder:
docker build --tag tagone -f Project/Dockerfile .
I'm trying to set up my dotnet core app to run through docker on my mac. My Dockerfile 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:runtime
WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "backend.dll"]
When I run: docker build -t backend .
I get :
Sending build context to Docker daemon 1.133MB
Step 1/10 : FROM microsoft/dotnet:sdk AS build-env
---> 76d532b4a0ca
Step 2/10 : WORKDIR /app
---> Using cache
---> 9c04cc3784c1
Step 3/10 : COPY *.csproj ./
---> Using cache
---> 18b53c11abd6
Step 4/10 : RUN dotnet restore
---> Running in 46f3843faad7
Aborted
The command '/bin/sh -c dotnet restore' returned a non-zero code: 134
I did some debugging and noticed when I ran any command of dotnet it bombs. I ran dotnet --info and it showed the information but also Aborted. Any suggestions would be greatly appreciated.