Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

MSB1011 when dotnet build or publish runs inside a Docker build

ErrorMSB1011

Short answer

dotnet build and dotnet publish pick a project by searching the current folder. If a COPY step puts more than one project or solution file in that folder, MSBuild cannot choose. Name the exact .csproj or .sln file in the command, or change WORKDIR to the folder with a single project.

  • Applies to .NET SDK 8, .NET SDK 9, Docker Engine 27
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the project and solution files in the folder where the failing RUN step runs. Add a temporary RUN ls line before it, or check the folder on disk.

    RUN pwd && ls -1 *.sln *.slnx *.csproj
  2. 2Match what you see, then jump to its fix

    1. 1dotnet build or dotnet publish is written without a project or solution pathThe command has no file argument and the folder holds severalFix
    2. 2The Dockerfile copies the entire repo into /src and runs dotnet thereCOPY . . and WORKDIR put the whole repository in one folderFix
    3. 3The folder has MyApp.sln and MyApp.slnxBoth .sln and .slnx exist for the same solutionFix

What does the error look like?

  • MSB1011
  • dotnet build or dotnet publish fails in a RUN step of the Dockerfile
  • the same command works on your machine inside the project folder

Cause 1 of 3

The command has no file argument and the folder holds several

1Check

Without a file, MSBuild searches the current directory for a project or solution. A folder with a .sln and a .csproj, or with several projects, has no single answer. A .sln and a .slnx of the same solution also count as two.

ls -1 *.sln *.slnx *.csproj

2Fix

Pass the one file you want to build.

RUN dotnet restore MyApp.sln
RUN dotnet publish src/MyApp.Web/MyApp.Web.csproj -c Release -o /app/publish

3Verify

Rebuild the image.

docker build -t my-app .

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

COPY . . and WORKDIR put the whole repository in one folder

1Check

A repository root usually holds a solution file next to project folders, and sometimes a second project file. After COPY . . the working directory is the repository root, so a bare dotnet command sees every file at that level.

ls -1 *.sln *.csproj

2Fix

Move into the project folder before the command, or give the project path. Use the path form so it works from the repository root.

WORKDIR /src
COPY . .
WORKDIR /src/src/MyApp.Web
RUN dotnet publish -c Release -o /app/publish

3Verify

The publish step prints the output folder and the build finishes.

docker build --progress=plain -t my-app .

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

Both .sln and .slnx exist for the same solution

1Check

MSBuild documents that when both formats of a solution are present in one folder, you must name one of them.

ls -1 *.sln *.slnx

2Fix

Name the one you use in the command, or delete the one you do not use from the repository.

RUN dotnet build MyApp.slnx -c Release

3Verify

Rebuild the image.

docker build -t my-app .

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Always give dotnet build, restore and publish an explicit project or solution path in Dockerfiles.
  • Do not use -o with a solution path; the SDK documents that it is not allowed for solutions. Publish a single project instead.
  • Keep one solution format in the repository.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.