0

I have made a asp.net core 2.0 web API. Now I want to create a docker image for this API.

Here is my docker file content -

    FROM  microsoft/aspnetcore:2.0-nanoserver-1803 AS build-env
WORKDIR /app

FROM microsoft/aspnetcore-build:2.0-nanoserver-1803 AS base
WORKDIR /src

# 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 base AS final
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Microservice_Orders.dll"]

I am running the following command to create an image

docker build -t ms_orders .

But I am getting following error on execution.

******Step 11/12 : COPY --from=build-env /app/out . COPY failed: CreateFile \?\Volume{a3251a00-f8f4-4510-8db2-f495f33ce178}\app\out:

The system cannot find the file specified.******

Hershika Sharma
  • 105
  • 1
  • 14

1 Answers1

1

I found a few things to be a bit strange in that dockerfile.

It looks like you have base and build-env mixed up, i.e. you're building into base, but then trying to copy what you built from build-env.

Also the path from dotnet publish doesn't match the path in COPY --from=build-env. /src/out (I think) vs /app/out

I would try to change it to below. I have copied the dockerfile and commented where I've made changes.

FROM  microsoft/aspnetcore:2.0-nanoserver-1803 AS base  # name changed since this is your base
WORKDIR /app
EXPOSE 80   # You'll probably also need to expose the app on some port(s)
EXPOSE 443  # depending on if you want https or not, you can remove one of these lines

FROM microsoft/aspnetcore-build:2.0-nanoserver-1803 AS build-env # name changed since this is where you build
WORKDIR /src

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o /app/out           # output path changed!

# Build runtime image
FROM base AS final
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Microservice_Orders.dll"]

Disclaimer: I haven't actually tried to build it, so there could be a few more issues.

Christian Fosli
  • 1,646
  • 9
  • 14
  • Just a sidenote, not really related to your question: The base docker image you are using (microsoft/aspnetcore:2.0* from Docker Hub) has been deprecated, since dotnet core 2.0 is no longer supported by Microsoft. You might want to look into migrating your application to a newer version of dotnet core. – Christian Fosli Jul 26 '20 at 10:12
  • Thanks. .. It worked. If you could also explain the steps meaning - that would be great. As I am new to using docker – Hershika Sharma Jul 26 '20 at 16:34
  • Great! I think this article describes multi-stage builds very well: https://docs.docker.com/develop/develop-images/multistage-build/ Let me know if it's still unclear after reading through that – Christian Fosli Jul 26 '20 at 19:40
  • Basically, the purpose of splitting the dockerfile into several stages (`base`, `build-env`, and `final`) is to reduce the size of the final docker image. Note that we copy all the source code in the `build-env` stage, but we only include what is needed to run the application in the final image. – Christian Fosli Jul 26 '20 at 19:44