Tailored to multi-project solutions:
Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS builder
WORKDIR /app
COPY <other-proj-dir>/*.csproj <other-proj-dir>/
COPY <web-proj-dir>/*.csproj <web-proj-dir>/
RUN dotnet restore <web-proj-dir>
COPY . .
RUN dotnet publish <web-proj-dir> -c Release -o out --no-restore
FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine
WORKDIR /app
COPY --from=builder /app/out .
EXPOSE 80
CMD [ "dotnet", "<web-proj-name>.dll" ].dockerignore:
Dockerfile
.dockerignore
.git
.gitignore
.gitattributes
.idea
.vs
.vscode
**/obj
**/bin
**/*.*proj.user
**/.env
- The
Dockerfileshould be in the same folder as the.slnfile. See this. - See this for why we first copy the
.slnand.csprojfiles and dodotnet restoreinstead of copying the entire source code in one go. - When copying the
.csprojfiles, the original directory structure should be kept, because they (presumably) link to each other. Hence, our copying the.csprojfile in thelibdirectory, for instance, over to a directory of the same name in the container. - If you have so many
.csprojfiles that it's unfeasible toCOPYeach of them them manually like this, you can utilize certain techniques and tricks similar to the ones described in this article, and also this comment to automate this. But in our case, that adds too much complexity and we have few projects. - To explore the official .NET base images provided by Microsoft, and their use cases, see https://hub.docker.com/_/microsoft-dotnet.
- There's also the official dotnet/dotnet-docker respository, which contains sample apps and README guides, which I found helpful, this and this in particular.
- You can find official samples at https://github.com/dotnet/dotnet-docker/blob/main/samples/README.md
- The official examples all use the
ENTRYPOINTinstruction for running the application in the final stage, although I tested it withCMDand it works just as well. I read about the differences between the two, but I think in this particular use case, as far as I'm concerned they are functionally identical, and so we can use either one. I'm going withCMDjust to be consistent with my SvelteKit Dockerfile. Most Dockerfile examples for Node.js apps out there also useCMD.
For a single project: I'm putting this one here in case I need it in the future:
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS builder
WORKDIR /app
COPY *.csproj .
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o out --no-restore
FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine
WORKDIR /app
COPY --from=builder /app/out .
EXPOSE 80
CMD [ "dotnet", "<project-name>.dll" ]