m.tracey
m.tracey

Reputation: 333

Dockerfile copy function and .NET publish interaction

I am writing a small dockerfile to wrap my app. I am trying to mkdir in the container in order to copy things into the container. When I run the container it works but when I list the directories inside, the directories that I have tried to create dont exist. I have another dockerfile where I call exactly the same code without the .NET commands, so I imagine this has to do with the interaction between .NET publish and COPY.

Here is the dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS dev
RUN mkdir -p /app

COPY MyApp.WebUI/MyApp.WebUI.csproj /app/
RUN cd /app && dotnet restore

COPY . /app/
RUN cd /app && dotnet publish MyApp.sln -c Release -o out

RUN mkdir -p /startup

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
EXPOSE 80
COPY --from=dev /app/out .
ENTRYPOINT ["dotnet", "MyApp.WebUI.dll"]

It does build and run successfuly, but when I run docker exec -it my_container /bin/bash and then ls, I am unable to see either the 'app' directory or the 'startup' anywhere in the directory tree.

Is anybody able to explain this interaction or guide me as to where I may have gone wrong?

Upvotes: 0

Views: 1350

Answers (1)

Marco
Marco

Reputation: 23937

You are confusing the intermediate image dev with the final image, which copies the contents of the out-folder into the current workdir, ., which is not /app

Your startup folder is created relative to your current working directory, which is still the default one, not /app. To avoid this confusion your second command should be WORKDIR /app, to set the current working directory and amend all paths accordingly.

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS dev
WORKDIR /app

# ./ is now /app
COPY MyApp.WebUI/MyApp.WebUI.csproj ./ 
RUN dotnet restore

#copy the rest into your current working directory
COPY . ./
#since we are still in /app, we do not need cd /app any more
dotnet publish MyApp.sln -c Release -o out

# new stage 
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
# current working directory is ./
EXPOSE 80
# everything from your dev stage /app/out folder, where you published is copied to ./ in this stage
COPY --from=dev /app/out .
ENTRYPOINT ["dotnet", "MyApp.WebUI.dll"]

You can take the docker example as a reference point: https://docs.docker.com/engine/examples/dotnetcore/

Docs for multi-stage builds: https://docs.docker.com/develop/develop-images/multistage-build/

Upvotes: 2

Related Questions