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

Aidan Breen
2 min readNov 6, 2018

--

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.

--

--