Using Docker? Don’t forget to use build caching!

Aidan Breen
Nov 6, 2018 · 2 min read

Here’s a quick and easy tip that will speed up your docker builds for Python, Nodejs or any project that requires installing dependencies…

If you are installing dependencies, copy only your dependency list before installing (package.json in Node or requirements.txt in python with pip), then move your application code.

The struggle for relevant images continues…

Python example:

If your Dockerfile looks something like this:

FROM python:3.6-alpine
COPY . /app
RUN pip install -r /requirements.txt
CMD [ "python", "/app/yourscript.py" ]

Change it to:

FROM python:3.6-alpine
COPY requirements.txt /
RUN pip install -r /requirements.txt
COPY . /app
CMD [ "python", "/app/yourscript.py" ]

Node example:

If your Dockerfile looks something like this:

FROM node:8
COPY . /app
RUN npm install --production
EXPOSE 3000
CMD ["node", "app/index.js"]

Change it to:

FROM node:8
COPY package.json /app/package.json
RUN cd /app; npm install --production
COPY . /app
EXPOSE 3000
CMD ["node", "app/index.js"]

Note for both Node and Python

In both Node and Python, the dependencies are stored in a file by name and version so that application code can be deployed without dependencies, which can be pulled in later. These dependency files only change when a new dependency is installed (or an old one is updated).

Docker is smart enough to know that if a file hasn’t changed, the build steps after it don’t need to be run again. So npm install and pip install (the slowest steps of the build) are skipped!

Conclusion

I’ve modified the example dockerfiles for brevity — removed comments and extra steps that I use in reality.

Got an example in another programming language? Let me know.

If you enjoyed this post, consider following me on twitter, or signup to my personal mailing list for less than monthly updates.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store