Dockerfile best practices to speed up builds #1453

Closed
opened 2025-08-09 17:20:12 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @aidansteele on 8/4/2016

I noticed that there was some room for improvement in the Dockerfiles created by the Yeomen generator. There are two parts to it:

  • Run the apt-get commands before any project-specific commands.
  • Copy only project.json before running dotnet restore, then copy the rest before dotnet build.

The rationale for both these changes is driven by how Docker caches intermediate images in building from a Dockerfile.

I won't duplicate that explanation here, but this means that Node.js won't need to be reinstalled on every code change and project dependencies won't need to be restored unless project.json changes.

I hope you find this useful. I wouldn't have been able to get started down the ASP.Net SPA path without the generators, so keep up the great work.

EDIT: I would have also added a .dockerignore to the templates, but I'm not at all familiar with Yeomen generators and wasn't sure if it was as simple as dropping it in the right place. It's essentially the same syntax as the .gitignore. We don't want any build artifacts from the Docker host making their way into the Docker container.

*Originally created by @aidansteele on 8/4/2016* I noticed that there was some room for improvement in the Dockerfiles created by the Yeomen generator. There are two parts to it: - Run the `apt-get` commands before any project-specific commands. - Copy _only_ `project.json` before running `dotnet restore`, then copy the rest before `dotnet build`. The [rationale](https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#/build-cache) for both these changes is driven by how Docker caches intermediate images in building from a Dockerfile. I won't duplicate that explanation here, but this means that Node.js won't need to be reinstalled on every code change and project dependencies won't need to be restored unless `project.json` changes. I hope you find this useful. I wouldn't have been able to get started down the ASP.Net SPA path without the generators, so keep up the great work. **EDIT**: I would have also added a .dockerignore to the templates, but I'm not at all familiar with Yeomen generators and wasn't sure if it was as simple as dropping it in the right place. It's essentially the same syntax as the .gitignore. We don't want any build artifacts from the Docker host making their way into the Docker container.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#1453
No description provided.