mpen
mpen

Reputation: 283293

Docker build not using cache

When I run

docker build -f docker/webpack.docker services/webpack --build-arg env=production

twice in a row, Docker builds my image each time, starting from the first RUN (the COPY uses the cache).

FROM node:lts
ARG env=production
ENV NODE_ENV=$env

WORKDIR /app

COPY package.json yarn.lock ./

RUN yarn install --frozen-lockfile --production=false --non-interactive

COPY . .

RUN node --max-old-space-size=20000 node_modules/.bin/svg2fonts icons -o assets/markons -b mrkn -f markons -n Markons
RUN node --max-old-space-size=20000 node_modules/.bin/webpack --progress

How can I get it to cache those RUNs?

Output looks like:

Sending build context to Docker daemon   3.37MB
Step 1/9 : FROM node:lts
 ---> 0c601cba9f11
Step 2/9 : ARG env=production
 ---> Using cache
 ---> dd38b2167c75
Step 3/9 : ENV NODE_ENV=$env
 ---> Using cache
 ---> 800f5afd416c
Step 4/9 : WORKDIR /app
 ---> Using cache
 ---> d15b93dce11d
Step 5/9 : COPY package.json yarn.lock ./
 ---> Using cache
 ---> a049dd1609a8
Step 6/9 : RUN yarn install --frozen-lockfile --production=false --non-interactive
 ---> Using cache
 ---> d5e51b0d556c
Step 7/9 : COPY . .
 ---> 92990e326d4b
Step 8/9 : RUN node --max-old-space-size=20000 node_modules/.bin/svg2fonts icons -o assets/markons -b mrkn -f markons -n Markons
 ---> Running in a23878db7b0e
Wrote assets/markons/markons.css
Wrote assets/markons/markons.js
Wrote assets/markons/markons.html
Wrote assets/markons/markons-chars.json
Wrote assets/markons/markons.svg
Wrote assets/markons/markons.ttf
Wrote assets/markons/markons.woff
Wrote assets/markons/markons.woff2
Wrote assets/markons/markons.eot
Removing intermediate container a23878db7b0e
 ---> 3bce79d0ecf0
Step 9/9 : RUN node --max-old-space-size=20000 node_modules/.bin/webpack --progress
 ---> Running in b6d460488950
<s> [webpack.Progress] 0% compiling
...

Upvotes: 0

Views: 539

Answers (1)

funnydman
funnydman

Reputation: 11376

See the description:

If the contents of all external files on the first COPY command are the same, the layer cache will be used and all subsequent commands until the next ADD or COPY command will use the layer cache.

However, if the contents of one or more external files are different, then all subsequent commands will be executed without using the layer cache.

So every time the content is changed two last RUN will be executed with no cache. There is no way to control caching yet. Maybe it's a better option to specify volumes?

Upvotes: 1

Related Questions