I have the following Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Ordering.API/Ordering.API.csproj", "Services/Ordering/Ordering.API/"]
COPY ["Ordering.Application/Ordering.Application.csproj", "Services/Ordering/Ordering.Application/"]
COPY ["Ordering.Core/Ordering.Core.csproj", "Services/Ordering/Ordering.Core/"]
COPY ["Ordering.Infrastructure/Ordering.Infrastructure.csproj", "Services/Ordering/Ordering.Infrastructure/"]
RUN dotnet restore "Services/Ordering/Ordering.API/Ordering.API.csproj"
COPY . Services/Ordering/
WORKDIR "/src/Services/Ordering/Ordering.API"
RUN dotnet build "Ordering.API.csproj" -c Release -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Ordering.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Ordering.API.Containerized.dll"]
And this is the project structure with relative layer's dependencies:
My question is:
how come that by just copying .csproj files into a Docker container the whole code is found/included in it? I mean the only code that is fully copied in Docker is Ordering.API code, from:
COPY . Services/Ordering/
How come that this project founds/resolves all the other code from the dependant layers (non Ordering.API projects) by just having available their .csproj files? Shouldn't the dotnet restore fails because code from dependant projects are not available in the container? The only code copied over is from Ordering.API.. I understand that the dependant .dlls are also built and included in the Ordering.API dll but how can these .dlls are created at all if their "meat" code is not imported in Docker (again, except for their .csproj file)? Sorry I am also new to Dockerfile.
Adding also an excerpt of docker-compose.yaml:
ordering.api:
image: ${DOCKER_REGISTRY-}orderingapi
build:
context: .
dockerfile: Ordering.API/Dockerfile
thanks.
I mean the only code that is fully copied in Docker is Ordering.API code, from:
COPY . Services/Ordering/
Check out the COPY command description:
COPY [--chown=<user>:<group>] [--chmod=<perms>] <src>... <dest>
TheCOPY
instruction copies new files or directories from<src>
and adds them to the filesystem of the container at the path<dest>
.
So it actually will copy everything from the docker build context which usually is the root folder of the repo. In your case it seems that the folder containing the Ordering.{ProjectName}
is the build context.
Check out the Properties for Dockerfile projects of the Container Tools build properties doc:
Property name | Description | Default value | NuGet package version |
---|---|---|---|
... | ... | ... | ... |
DockerfileContext | The default context used when building the Docker image, as a path relative to the Dockerfile. | Set by Visual Studio when Docker support is added to a project. In .NET Framework projects, set to "." (the project folder), and in .NET Core and .NET 5 and later projects, it's set to the relative path to the solution folder (usually ".."). | 1.0.1872750 or newer |