Overcoming `npm install` in Docker...

August 15, 2017

At Ygrene, we build javascript code all day long. We do that with the help of Bitbucket, Bamboo CI server, and Docker. As many of you know, the first step in any modern javascript build is npm install. This fills your drive thousands of tiny javascript files, executables and whatever else. Each one of those files has a complete set of metadata and permissions assignments. When using Docker you will notice that all your commands run as root by default. This causes file permissions errors. Due to this fact, created files will now have a root or ‘0’ uuid the surrounding CI server thinks they belong to root. For subsequent npm scripts the errors were of the class:

npm ERR! Error: EACCES: permission denied

We needed to find a solution to this problem. In previous versions of Docker, our team had to create delegation scripts. These scripts took your command as a string, changed users and ran it. We needed our commands to execute as a user who could access any created files from the lifetime of the command. These workarounds required base images that held the scripts. This narrowed the images we could pull from Docker hub, and complicated the use when we did. Thanks to newer versions of Docker we can remove the scripts and use any Docker Hub image. By throwing the  — user argument to docker run, we are able to enforce that the command runs under the uuid we passed in. Your needs may vary, so Docker has provided flexibility in the run command. For more info check out the Docker run — user docs.

Since we use the newest version of bamboo, we can take advantage of Bamboo Specs. Here is a Java snippet for how we make Docker containers run on bamboo.

While you are down here: All opinions are my own, and do not necessarily reflect those of my employer.

Overcoming npm install in Docker was originally published in Ygrene Tech on Medium, where people are continuing the conversation by highlighting and responding to this story.