Dockerfile Caching
Structuring your Dockerfile to maximize efficiency is key to making Dockerfile Deploy faster.
Bundler
In order for the Docker build cache to cache gems installed via Bundler, it's necessary to add the Gemfile and Gemfile.lock files to the image, and run bundle install
, before adding the rest of the repo (via ADD .
). Here's an example of how that might look in a Dockerfile:
FROM ruby
# If needed, install system dependencies here
# Add Gemfile and Gemfile.lock first for caching
ADD Gemfile /app/
ADD Gemfile.lock /app/
WORKDIR /app
RUN bundle install
ADD . /app
# If needed, add additional RUN commands here
NPM
In order for the Docker build cache to cache packages installed via npm, it's necessary to add the package.json
file to the image, and run npm install
, before adding the rest of the repo (via ADD .
).
Here's an example of how that might look in a Dockerfile:
FROM node
# If needed, install system dependencies here
# Add package.json before rest of repo for caching
ADD package.json /app/
WORKDIR /app
RUN npm install
ADD . /app
# If needed, add additional RUN commands here
PIP
In order for the Docker build cache to cache packages installed via pip, it's necessary to add the requirements.txt
file to the image, and run pip install
, before adding the rest of the repo (via ADD .
).
Here's an example of how that might look in a Dockerfile:
FROM python
# If needed, install system dependencies here
# Add requirements.txt before rest of repo for caching
ADD requirements.txt /app/
WORKDIR /app
RUN pip install -r requirements.txt
ADD . /app
Updated about 2 years ago