Windows容器指定文件无法找到及/app目录缺失问题求助
Hey there! No worries at all—we all start somewhere with Docker, and this is a super common gotcha. Let’s break down what’s happening here.
Why the /app directory isn’t showing up
The root issue here is almost certainly that your COPY dist /app instruction didn’t actually create the /app directory in the image. Here are the most likely reasons:
Your local
distdirectory is empty or doesn’t exist
Docker’sCOPYcommand only creates the target directory if there’s content to copy into it. If your localdistfolder is empty, or you’re runningdocker buildfrom a directory wheredistdoesn’t exist, Docker won’t create the/appdirectory at all. That’s whydocker diffdoesn’t show it, yourdocker cpcommand fails, and you get the startup error (the container can’t findExampleApp.dllbecause/appdoesn’t exist).You’re mixing Linux and Windows container modes
Themcr.microsoft.com/dotnet/core/aspnet:3.1image defaults to a Linux-based image. If you’re running Docker in Windows Container mode on your machine, this causes path mismatches. Windows containers use backslash paths (likeC:\app) instead of forward slashes (/app), so settingWORKDIR /apppoints to a non-existent path relative to the current drive.
How to fix this
Let’s walk through the steps to resolve this:
1. Make sure your dist directory has content
First, generate the published files for your .NET app if you haven’t already. Run this in your local project directory:
dotnet publish -c Release -o dist
This creates a dist folder with all necessary files (including ExampleApp.dll).
2. Build your image correctly
Navigate to the folder where your Dockerfile lives (it should be in the same parent directory as your dist folder), then run:
docker build -t exampleapp .
The . at the end tells Docker to use the current directory as the build context, so it can find the dist folder.
3. Match your container mode to the base image
- If you want to use Linux containers (default for this .NET image), open Docker Desktop > Settings > General and ensure "Use the WSL 2 based engine" is enabled.
- If you prefer Windows containers, switch Docker to Windows mode and update your Dockerfile to use a Windows-specific base image and paths:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1909 COPY dist C:\app WORKDIR C:\app EXPOSE 80/tcp ENTRYPOINT ["dotnet", "ExampleApp.dll"]
4. Verify the directory exists
After building the corrected image, run a temporary container to check if the directory has your files:
# For Linux containers docker run --rm exampleapp ls /app # For Windows containers docker run --rm exampleapp dir C:\app
This should list ExampleApp.dll and other published files. If it does, your docker cp command will work once you start a container from this image.
Bonus debugging tips
- Always check the output of
docker build—if there’s an error about missingdist, that’s your immediate clue. - Get an interactive shell inside the image to explore the filesystem directly:
# Linux containers docker run --rm -it exampleapp bash # Windows containers docker run --rm -it exampleapp cmd
内容的提问来源于stack exchange,提问作者whoisit

