Maaz Zindani

Loading the index…

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

docker build fails with CS5001, no entry point found

Errorerror CS5001: Program does not contain a static 'Main' method suitable for an entry point

Short answer

CS5001 means the compiler built an executable project and found no entry point. In a Docker build that is usually a source file that never reached the image, a project that is meant to be a library, or two Main methods with no StartupObject. Fix the project's OutputType and entry point, then check what COPY really sent.

  • Applies to .NET SDK 9, C# 13, Docker 27
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Build the same project outside Docker. If it fails there too, the cause is the project. If it only fails in the image, the cause is what got copied.

    dotnet build -c Release
    grep -n "OutputType\|StartupObject" *.csproj
  2. 2Match what you see, then jump to its fix

    1. 1CS5001 only inside docker build, while the same project builds on your machineThe project is an exe but no file with an entry point reached the compilerFix
    2. 2CS5001 after changing OutputType, or when dotnet publish runs in a folder that holds a library projectThe project you publish is a class libraryFix
    3. 3CS5001 or CS1558 with a Main method present in the codeThe Main method has a signature the compiler does not acceptFix
    4. 4CS0017: Program has more than one entry point definedTwo entry points in one projectFix

What does the error look like?

  • error CS5001: Program does not contain a static 'Main' method suitable for an entry point
  • error CS0017: Program has more than one entry point defined.

Cause 1 of 4

The project is an exe but no file with an entry point reached the compiler

1Check

COPY can only send files that are inside the build context and not excluded by .dockerignore. A pattern that hides Program.cs, or a COPY that points at the wrong folder, leaves the compiler with no entry point. List what the image stage really contains.

cat .dockerignore
docker build --target build -t entry-check .
docker run --rm --entrypoint ls entry-check -R /src

2Fix

Make sure the folder holding Program.cs is copied and not matched by .dockerignore, and that COPY paths are relative to the context root you pass to docker build.

# .dockerignore: exclude build output, not source
**/bin/
**/obj/

# Dockerfile
COPY . .
RUN dotnet publish -c Release -o /app

3Verify

Rebuild the image without cache and watch the publish step finish.

docker build --no-cache -t myapp .

Confirmed by docs.docker.com

Did this fix it?

Cause 2 of 4

The project you publish is a class library

1Check

A project with OutputType exe or winexe needs an entry point. A library does not. Check which project the Dockerfile publishes and what its OutputType is.

grep -n "OutputType" *.csproj
grep -n "dotnet publish\|dotnet build" Dockerfile

2Fix

Publish the project that has the entry point. If a project should be a library, set its output type to Library so the compiler stops asking for Main.

<PropertyGroup>
  <OutputType>Library</OutputType>
</PropertyGroup>

3Verify

Publish the intended web or console project and confirm its dll is in the output folder.

dotnet publish MyApp.Web/MyApp.Web.csproj -c Release -o out

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 4

The Main method has a signature the compiler does not accept

1Check

The entry point must be static, return void, int, Task or Task<int>, and may take a string[] parameter. The name is case-sensitive, so a method called main does not count. An async Main needs C# 7.1 or higher.

grep -rn "static.* Main\|static.* main" --include=*.cs .

2Fix

Correct the signature, and move Main out of any generic type.

static async Task Main(string[] args)
{
    await Task.CompletedTask;
}

3Verify

Build again.

dotnet build -c Release

Confirmed by learn.microsoft.com

Did this fix it?

Cause 4 of 4

Two entry points in one project

1Check

A second Main, for example in a tool or sample class that was copied into the same project, makes the compiler unable to choose.

grep -rn "static.* Main(" --include=*.cs .

2Fix

Remove the extra Main, or tell the compiler which type holds the real one with StartupObject. If the file uses top-level statements, remove the explicit Main instead, because the compiler ignores it.

<PropertyGroup>
  <StartupObject>MyApp.Program</StartupObject>
</PropertyGroup>

3Verify

Build again and confirm CS0017 is gone.

dotnet build -c Release

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Build and publish the same project locally with the exact command from the Dockerfile before pushing.
  • Keep .dockerignore limited to build output (bin and obj), logs and secrets.
  • Put one explicit project path in dotnet publish when a repository holds several projects.
  • Pass the context root once and keep every COPY path relative to it.

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.