2

I have all this npm stuff in my Dockerfile and it is taking a long time to build my docker image. How can I speed this up, and ideally cache the results? Nothing is changing, so I wouldn't expect this to take such a long time (about 20 seconds now).

FROM python:3.6-alpine

# python stuff
COPY requirements.txt /app/requirements.txt
RUN pip3 install --upgrade pip
RUN pip3 install -r /app/requirements.txt

# npm stuff
RUN apk add --update nodejs-npm
RUN npm init -y
RUN npm i webpack webpack-cli --save-dev
RUN npm i @babel/core babel-loader @babel/preset-env @babel/preset-react babel-plugin-transform-class-properties --save-dev
RUN npm i react react-dom prop-types --save
RUN npm i react-bootstrap bootstrap
RUN npm i weak-key --save

I did try this solution using the COPY package.json but babel and webpack did not seem to like that (and it did not work).

NOTE: I need to use python:3.6-alpine as this is an existing Django application that is integrating React.js

Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
  • Do you have any COPY commands or other commands that break the docker cache above the RUN commands you show? – shadowspawn Mar 01 '19 at 22:39
  • 1
    Did you try to minimize the use of RUN (every RUN its build new temp container, which is making it slow)? That instead of using RUN for every command, to have few commands on the same RUN - like "RUN npm init -y & npm i webpack ... & ...". You can still separate them to different lines by using "\" at the end of each line. – Yagel Mar 01 '19 at 23:05
  • @shadowspawn - I have `COPY requirements.txt /app/requirements.txt` above the run commands. Why does this break the docker cache? – Scott Skiles Mar 04 '19 at 13:35
  • Docker layer cache: see "Leverage build cache" on https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ – shadowspawn Mar 06 '19 at 22:51
  • The docker layer cache should make it fast to build if nothing has changed, on the same computer, except for the initial directory scan. For that, see "Understand build context" on https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ – shadowspawn Mar 06 '19 at 22:54
  • 1
    If you used a package.json (and package-lock.json) rather than hand installing dependencies, you could use "npm ci" rather than "npm install": https://docs.npmjs.com/cli/ci.html – shadowspawn Mar 06 '19 at 23:01
  • Note: @Yagel suggested the simplest win, combining the RUN commands into single RUN. I'm going wider as wondering what is actually slow and what you could do differently. :-) – shadowspawn Mar 06 '19 at 23:04
  • @Yagel - That did help quite a bit but I think I need to figure out how to cache that (or avoid breaking the cache). I'll take a look at your recommendations shadowspawn – Scott Skiles Mar 07 '19 at 20:41

0 Answers0