Docker Dockerfile contents ordering

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


  1. Base image declaration (FROM)
  2. Metadata (e.g. MAINTAINER, LABEL)
  3. Installing system dependencies (e.g. apt-get install, apk add)
  4. Copying app dependencies file (e.g. bower.json, package.json, build.gradle, requirements.txt)
  5. Installing app dependencies (e.g. npm install, pip install)
  6. Copying entire code base
  7. Setting up default runtime configs (e.g. CMD, ENTRYPOINT, ENV, EXPOSE)

These orderings are made for optimizing build time using Docker's built-in caching mechanism.

Rule of thumbs:

Parts that change often (e.g. codebase) should be placed near bottom of Dockerfile, and vice-versa. Parts that rarely change (e.g. dependencies) should be placed at top.

Got any Docker Question?