4.1.1.3 Dockerfile Dependency Install
A focused guide to Dockerfile Dependency Install, connecting core concepts with practical Docker and container operations.
Dockerfile dependency installation is the step, or steps, in a Dockerfile responsible for installing everything the application needs beyond what its base image already provides — system packages, language-specific libraries, or other software dependencies — typically using the package manager appropriate to the base image or language ecosystem in use.
Installing System-Level Dependencies
System-level dependencies are typically installed through the base image's own package manager, using whatever syntax that distribution's tooling requires.
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y libpq5 curl && rm -rf /var/lib/apt/lists/*
Combining the update, install, and cleanup into a single RUN instruction keeps the resulting layer's size limited to the net effect of these combined operations.
Installing Language-Specific Dependencies
Most language ecosystems provide their own dependency management tooling, typically driven by a manifest file (requirements.txt, package.json, pom.xml) that lists exactly what the application needs.
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
Ordering Dependency Installation for Cache Efficiency
Copying only the dependency manifest before running the installation step, and copying the rest of the application source afterward, lets the build cache reuse the (often slow) dependency installation step across builds that only change application code.
COPY package*.json ./
RUN npm install
COPY . .
If only application source changes between builds, this ordering avoids unnecessarily reinstalling dependencies that have not actually changed.
Pinning Dependency Versions for Reproducibility
Installing dependencies from a lockfile, rather than allowing a package manager to resolve to whatever versions happen to be available at build time, keeps builds reproducible across time.
RUN npm ci
npm ci installs strictly according to the lockfile, in contrast to npm install, which can resolve to newer compatible versions than what the lockfile specifies.
Why Dependency Installation Steps Matter
The dependency installation step is often the most time-consuming and most frequently cache-relevant part of a typical build, making how it is structured — what is copied beforehand, how versions are pinned, how cleanup is handled — one of the most impactful areas for both build speed and final image correctness.