DockerDockerfile contents ordering


  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.