Docker + Node.js is a beautiful combination. But among all of the advantages of using these tools together, there are some mild frustrations… things that just don’t quite fit nicely.
Take the npm Wall of Red Text, for example.
It seems every time I run ‘npm install’ inside of my docker container, I nearly have a panic attack thinking my build is failing!
It’s nerve-racking to say the least… is my build failing? Did it fail? Or is this just the npm Wall of Red Text again?
9 out of 10 times, it ends up “npm info ok”, of course, but that doesn’t make me feel any better about the Wall…
Fortunately, we can work around this anxiety inducing Wall of Red Text with a few npm configuration options. The options you set will be dependent on what you do and don’t want to see.
Too Much “info”
The first thing I want to get rid of is the wall of text. Honestly, I just don’t care about seeing all of the information supplied.
The default npm log level for a Docker image is “info” instead of “warn” like it would be on your machine. To fix that, adjust one of two options in your Dockerfile:
In this Dockerfile example, I have set both the “NPM_CONFIG_LOGLEVEL” environment variable and “–loglevel” command-line parameter to “warn”.
You don’t need both of these options in the Dockerfile, though. Pick whichever one works best for you in a given situation.
If you find yourself needing to run npm install multiple times, for example, and you want to ensure npm is always set to “warn”, then I would set the environment variable. This is a great option for a development Dockerfile when you know you’ll be shelled into a container to work with npm. Just be sure to set the ENV instruction before you run npm install, or it won’t have any effect on your Docker build process.
If you only want one specific instance of npm install to have a given log level, however, you can adjust that one call with the “–loglevel” parameter, as I’ve shown.
The end result is a much more manageable output, with only warning and error messages shown… basically, duplicating what you would see on your computer.
But not everyone wants to reduce the output of their build. Maybe you just want to get rid of the red text.
Too Much “color”
If you prefer to see the wall of text, but don’t want to see red all the time, you can adjust the “color” configuration setting.
Again, you have two options. Set the “NPM_CONFIG_COLOR” environment variable, or set the “–color” command-line parameter.
The use of these options is basically the same as the previous example. Decide when you want to change the color setting and use the option that is appropriate for your situation.
With the “color” option set to false, as shown, npm will not use any color coding for the log output.
Unfortunately, this means your warning and error messages won’t be in color, either. But if you don’t mind that limitation and you do want to see the wall of text, this is a good option.
Can I Get A Red Warning and White Wall?
It’s likely that there is some incompatibility between the Docker log output and npm which is causing the npm Wall of Red Text to begin with. And whatever is causing this is preventing me from getting the output that I really want: white info and red warning text.
Personally, I prefer to set the loglevel to “warn” so this isn’t a huge problem for me. There are times, however, when I want to see the “info” level output and I wish there were a way to get “info” in white and “warn” in red, when using npm with Docker.
For now, though, you’ll have to work with the combination of “loglevel” and “color” in your Docker setup with npm.
That’s Great, But How Do I Not Run “npm install” So Often?
Having color and log level control with npm is great. But that doesn’t solve the larger problem of how often “npm install” runs when working with Docker.
It seems everything you do, every time you even think about touching your Docker project, you end up running “npm install” again. It can be a slow, life-draining experience when it happens multiple times per hour.
Fortunately, there are solutions for this as well. With a small adjustment to your Dockerfile and a clever use of Docker volumes, you can create a node modules cache that will easily cut 80% of the “npm install” from your Docker build.
And I want to show you how to do exactly that – to eliminate the npm install delay from your Docker project almost entirely.