0

I'm looking at this tutorial: https://learn.microsoft.com/en-us/learn/modules/implement-docker-multi-stage-builds/3-examine-multi-stage-dockerfiles

And this part confuses me:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["WebApplication1.csproj", ""]
RUN dotnet restore "./WebApplication1.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build

Why would you use WORKDIR twice here? Aren't we already in the src?
Does the dot at the end (/src/.) has any additional meaning?

Mr Patience
  • 1,564
  • 16
  • 30
  • 1
    My read is also that the second `WORKDIR` line does nothing. I could see it making sense if there wound up being multiple projects in the container filesystem or if the `.csproj` file wasn't in the `/src` directly for some reason. – David Maze May 21 '22 at 10:59

1 Answers1

0

Adding twice WORKDIR is for clarity. As best practise adding WORKDIR with absolute path is recommended for readability and maintenance purpose.

Working directory can also be verify as below

docker run -it imageName pwd

replace imageName -with actual Image Name

pwd with cd (windows current directory command)

Link for Docker best practises, refer WORKDIR section.

kus
  • 446
  • 3
  • 7
  • Sorry, but how is that more clear? Can you also provide a source of `best practice` claim? – Mr Patience May 21 '22 at 15:29
  • updated with link. above example is related to multi stage, simple and readable. what if this contain much more lines of code, then it become bit complex. This is recommended, ofcourse in above example its's not adding much value. – kus May 21 '22 at 17:26