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.

Simple Dockerfile

# Base image
FROM python:2.7-alpine

# Metadata
MAINTAINER John Doe <[email protected]>

# System-level dependencies
RUN apk add --update \
    ca-certificates \
    && update-ca-certificates \
    && rm -rf /var/cache/apk/*

# App dependencies
COPY requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

# App codebase
COPY . ./

# Configs
CMD ["python", ""]

MAINTAINER will be deprecated in Docker 1.13, and should be replaced by using LABEL. (Source)

Example: LABEL Maintainer="John Doe [email protected]"