recently , a new package manager built on top of the npm registry, massively reducing install times and shipping a deterministic build out of the box. Facebook released Yarn Node.js Determinism has always been a problem with npm, and solutions like are not . This makes hard to use a -based system for multiple developers and on continuous integration. Also, slowness in case of complex files causes long build times, representing a serious blocker when using Docker for local development. npm shrinkwrap working well npm npm package.json This article discuss how to use Yarn with Docker for Node.js development and deployment. take on installing code xkcd TL;DR Clone the boilerplate: git clone https://github.com/mfornasa/DockerYarn.git Enter the directory: cd DockerYarn Build the container: ./build.sh Run it: docker run yarn-demo node -e "console.log('Hello, World')" The first time your build the container, Yarn fetches dependencies for you. After that, Yarn is executed only when you modify your , and it uses cache from previous executions. On top of it, you have : the same dependency tree is installed every time and on every machine. And ! npm package.json determinism it’s blazing fast Let’s get started The procedure works on Mac and Linux. We are going to the Node.js Docker image for Node 6. Please on your machine before proceeding. Risingstack install Yarn Download Yarn installation package in a local folder: wget https://yarnpkg.com/latest.tar.gz Create a new : Dockerfile FROM risingstack/alpine:3.4-v6.7.0-4.0.0 WORKDIR /opt/app # Install yarn from the local .tgzRUN mkdir -p /optADD latest.tar.gz /opt/RUN mv /opt/dist /opt/yarnENV PATH "$PATH:/opt/yarn/bin" # Install packages using YarnADD package.json /tmp/package.jsonRUN cd /tmp && yarnRUN mkdir -p /opt/app && cd /opt/app && ln -s /tmp/node_modules This is based on a trick to make use of Docker layer caching to avoid to reinstall all your modules each time you build the container. In this way, Yarn is executed (and the first time, of course). well-known only when you change **package.json** Init package.json yarn init Add your first package: yarn add react Build and run your new container: docker build . -t yarn-demodocker run yarn-demo node -e "console.log('Hello, World')" Congratulations! You’re using with Docker. yarn Wait! What about " ? yarn.lock” Yarn stores the exact version of each package and sub-package in order to be able to reproduce exactly the same dependency tree on each run. Both and must be checked into source control. As we run Yarn inside the container, we need to retrieve . Luckily, it’s not hard to extract after each run. Simply change the line in the with the following: package.json yarn.lock yarn.lock yarn.lock ADD Dockerfile ADD package.json yarn.lock /tmp/ and build the container using the following command: docker build . -t yarn-demo; docker run --rm --entrypoint cat yarn-demo:latest /tmp/yarn.lock > yarn.lock After the build, is copied to your working directory, and it will be reused on next Docker run, installing the same dependencies each time. yarn.lock Congratulations! Now you have Yarn execution. deterministic Wait! Now Yarn is executed at each container build That is correct, we are now running Yarn at each build, even if has not been modified. This is because is copied from the container to your working directory each time, even if it’s not changed, thus invalidating Docker layer caching. To solve this, we need to copy only if it’s really changed. To do so: package.json yarn.lock yarn.lock Create a file: build.sh #!/bin/bash docker build . -t yarn-demo docker run --rm --entrypoint cat yarn-demo:latest /tmp/yarn.lock > /tmp/yarn.lockif ! diff -q yarn.lock /tmp/yarn.lock > /dev/null 2>&1; thenecho "We have a new yarn.lock"cp /tmp/yarn.lock yarn.lockfi Make it executable: chmod +x build.sh Use it to build the container: ./build.sh Then run the container: docker run yarn-demo node -e "console.log('Hello, World')" Congratulations! You have now a Yarn execution, and Yarn is executed . deterministic only when you change **package.json** What about Yarn package cache? Another powerful feature of Yarn is package cache, which is stored on the local filesystem, to avoid downloading packages again. Our procedure so far does not maintain cache over container builds. This could be an issue for big files. package.json The following solves the issue by saving Yarn cache on your working directory. build.sh #!/bin/bash # Init empty cache fileif [ ! -f .yarn-cache.tgz ]; thenecho "Init empty .yarn-cache.tgz"tar cvzf .yarn-cache.tgz --files-from /dev/nullfi docker build . -t yarn-demo docker run --rm --entrypoint cat yarn-demo:latest /tmp/yarn.lock > /tmp/yarn.lockif ! diff -q yarn.lock /tmp/yarn.lock > /dev/null 2>&1; thenecho "Saving Yarn cache"docker run --rm --entrypoint tar yarn-demo:latest czf - /root/.yarn-cache/ > .yarn-cache.tgzecho "Saving yarn.lock"cp /tmp/yarn.lock yarn.lockfi You also need to add this to your , after the line: Dockerfile ADD package.json... # Copy cache contents (if any) from local machineADD .yarn-cache.tgz / The cache file is not meant to be pushed to the repo, so it should be added to a file. .gitignore Congratulations, again! You have now a Yarn execution, which is executed , and it uses . Try this with a complex file from a real project, you will be amazed! deterministic only when you change **package.json** Yarn caching package.json For more pieces on DevOps and Docker, join my . If you enjoyed this piece click the “ ♥︎ ” button below. mailing list