Using Yarn with Docker

Martino Fornasa
HackerNoon.com
5 min readOct 13, 2016

--

Facebook recently released Yarn, a new Node.js package manager built on top of the npm registry, massively reducing install times and shipping a deterministic build out of the box.

Determinism has always been a problem with npm, and solutions like npm shrinkwrap are not working well. This makes hard to use a npm-based system for multiple developers and on continuous integration. Also, npm slowness in case of complex package.json files causes long build times, representing a serious blocker when using Docker for local development.

This article discuss how to use Yarn with Docker for Node.js development and deployment.

xkcd take on installing code

TL;DR

  • Clone the boilerplate:
  • Enter the directory:
  • Build the container:
  • Run it:

The first time your build the container, Yarn fetches npm dependencies for you. After that, Yarn is executed only when you modify your package.json, and it uses cache from previous executions. On top of it, you have determinism: the same dependency tree is installed every time and on every machine. And it’s blazing fast!

Let’s get started

The procedure works on Mac and Linux. We are going to the Risingstack Node.js Docker image for Node 6. Please install Yarn on your machine before proceeding.

  • Download Yarn installation package in a local folder:
  • Create a new Dockerfile:

This is based on a well-known 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 only when you change package.json (and the first time, of course).

  • Init package.json
  • Add your first package:
  • Build and run your new container:

Congratulations! You’re using yarn with Docker.

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 package.json and yarn.lock must be checked into source control. As we run Yarn inside the container, we need to retrieve yarn.lock. Luckily, it’s not hard to extract yarn.lock after each run. Simply change the ADD line in the Dockerfile with the following:

and build the container using the following command:

After the build, yarn.lock is copied to your working directory, and it will be reused on next Docker run, installing the same dependencies each time.

Congratulations! Now you have deterministic Yarn execution.

Wait! Now Yarn is executed at each container build

That is correct, we are now running Yarn at each build, even if package.json has not been modified. This is because yarn.lock 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 yarn.lockonly if it’s really changed. To do so:

  • Create a build.sh file:
  • Make it executable:
  • Use it to build the container:
  • Then run the container:

Congratulations! You have now a deterministic Yarn execution, and Yarn is executed 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 package.json files.

The following build.sh solves the issue by saving Yarn cache on your working directory.

You also need to add this to your Dockerfile , after the ADD package.json... line:

The cache file is not meant to be pushed to the repo, so it should be added to a.gitignore file.

Congratulations, again! You have now a deterministic Yarn execution, which is executed only when you change package.json, and it uses Yarn caching. Try this with a complex package.json file from a real project, you will be amazed!

If you enjoyed this piece click the “♥︎” button below. For more pieces on DevOps and Docker, join my mailing list.

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--

Martino Fornasa
HackerNoon.com

Independent Consultant & Strategic Advisor | DevOps | Cloud Native — https://fornasa.it/