Skip to content

Instantly share code, notes, and snippets.

@elpddev
Last active December 24, 2019 16:21
Show Gist options
  • Save elpddev/0866497ebc37ec7da4414991414753a5 to your computer and use it in GitHub Desktop.
Save elpddev/0866497ebc37ec7da4414991414753a5 to your computer and use it in GitHub Desktop.
lerna docker image build with cache for packages package.json
FROM node:12.7.0-alpine as node

RUN mkdir -p /usr/src/repo

ADD lerna.json /usr/src/repo/                            
ADD package.json \         
  yarn.lock \            
  .yarnrc \              
  /usr/src/repo/                      

ADD packages-jsons.tar.gz /usr/src/repo

WORKDIR /usr/src/repo  

RUN yarn install --frozen-lockfile && yarn lerna bootstrap

# build steps

In order for all the *packages.json of all packages to be added to docker, we cant use ADD or COPY for multiple file as they don't support recreating unexisting paths like mkdir.

In my case we wanted to avoid using multiple ADD commands that generate a lote of intermediate images.

Another way is to generate a tar file that contains the package.json files and use the ADD command to extract it in the docker build phase.

GZIP=-n tar -czvf packages-jsons.tar.gz ./packages/*/package.json

The GZIP=-n is nessasery here because normally gzip would also include the timestamp for files. This will break the cache when doing docker ADD for the tar file. You can see this when doing md5sum check on the generate tar zip file. Each time a different hash because of the different zip file inside. When using the -n option, the same hash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment