Skip to content

Instantly share code, notes, and snippets.

@aradalvand
Last active April 11, 2022 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aradalvand/4418feaae47a7cdb9939be3787b54fe4 to your computer and use it in GitHub Desktop.
Save aradalvand/4418feaae47a7cdb9939be3787b54fe4 to your computer and use it in GitHub Desktop.
Dockerfile for multi-project ASP.NET Core applications

Tailored to multi-project solutions:

TL;DR:

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


How it works:

  • The Dockerfile should be in the same folder as the .sln file. See this.
  • See this for why we first copy the .sln and .csproj files and do dotnet restore instead of copying the entire source code in one go.
  • When copying the .csproj files, the original directory structure should be kept, because they (presumably) link to each other. Hence, our copying the .csproj file in the lib directory, for instance, over to a directory of the same name in the container.
  • If you have so many .csproj files that it's unfeasible to COPY each 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 ENTRYPOINT instruction for running the application in the final stage, although I tested it with CMD and 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 with CMD just to be consistent with my SvelteKit Dockerfile. Most Dockerfile examples for Node.js apps out there also use CMD.

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" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment