0
votes

I have a dockerfile like below:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /src

# Keep the project list and command dotnet restore identical in all 
Dockfiles to maximize image cache utilization
COPY eShop.sln .
COPY docker-compose.dcproj /src/
COPY src/OcelotApiGw/OcelotApiGw.csproj src/OcelotApiGw/
COPY src/Services/Catalog.Api.csproj src/Services/
COPY src/Services/Identity.Api.csproj src/Services/
COPY src/eShop/eShop.csproj src/eShop/
RUN dotnet restore eShop.sln

COPY . .
WORKDIR /src/eShop/
RUN dotnet publish --no-restore -c Release -o /app

FROM build AS publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "eShop.dll"]

But I received error: Step 6/20 : COPY eShop.sln . COPY failed: stat /var/lib/docker/tmp/docker-builder464115212/eShop.sln: no such file or directory

Why? help me, please.

2
looks like your Dockerfile and eShop.sln aren't in the same place. You're probably confused by the WORKDIR, which refers to the container, not local folder. Also just curious: why are you not using version 3.0?ziya

2 Answers

1
votes

I think it can be confusing to new starters to understand the basics and to get anything working initially. So I wanted to create a simple but complete example container with unit tests for ASP.NET using Docker multi-stage build.

Example project structure

First, a quick project to set the scene:

mkdir src && cd src
dotnet new web -o myweb.app
dotnet new xunit -o myweb.tests
dotnet new sln -n myweb
dotnet sln add myweb.app
dotnet sln add myweb.tests
cd ..
edit Dockerfile

You should end up with a Dockerfile and an src folder next to it.

Dockerfile

Here are the contents of the Dockerfile which uses three stages to build, test and prepare the runtime. Notice the FROM ... AS ... directives. Also, remember that WORKDIR refers to a path in the container being built, not your local path:

FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build

# copy and build everything
WORKDIR /app
COPY src .
RUN dotnet build -c Release

# publish our web app
WORKDIR /app/myweb.app
RUN dotnet publish -c Release -o out

# test everything
FROM build AS testrunner
WORKDIR /app
ENTRYPOINT ["dotnet", "test", "--logger:trx"]

# prepare runtime
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
WORKDIR /app
COPY --from=build /app/myweb.app/out .
ENTRYPOINT ["dotnet", "myweb.app.dll"]

Build and run:

docker build -t myweb .
docker run --rm --name myweb1 -p 8080:80 myweb

Your app sould be available on http://localhost:8080

There are more examples in the GitHub .NET repository's Docker samples folder if you want to check them out too.

PS Tried on my Mac but it should work on Windows or Linux too.

PPS I used .NET Core 3.0 since that is the recommended release as of this writing.

Hope it helps. Have fun!

0
votes

The issue is this line:

FROM build AS publish

Followed by:

COPY --from=publish /app .

You've created a new stage, where you've done nothing, and copied nothing into it, and then you're trying to copy from this empty stage. Remove this stage and then do:

COPY --from=build /app .