Dockerizing NPM
This article is part of a series in which I describe how I dockerize my development environment. So far, I’ve published the following articles:
The aim of this project is to simplify setting up my computer or any other system I work on. My current idea is to have a github repository that contains a script for setting up my environment and a couple of dotfiles that configure tools and create aliases.
The idea behind the following line of bash code is that you can use it to define an alias for running NPM. Before doing so, let’s see how this command works.
docker run --rm -it -w /opt/ -v $(pwd):/opt/ node npm
If you have no idea what Docker is, you should visit the official website and try it!
- docker run starts a new container
- the —rm option makes sure the container is deleted after has finished
- the -it options (-i and -t) let you interact with the container in an interactive mode via a pseudo-tty.
- -w sets the container’s commands working directory (this is where the command will create files — for example when running npm init)
- -v host-directory:container-directory mounts a directory from your host computer in the container
Note: when using boot2docker on Mac OS X, you can use volumes since Docker version 1.3. This however only works in a directory in /Users/.
- The parameter node describes what image to run. You can find all kinds of pre-built images at registry.hub.docker.com. The node image is an official one which can be used to run node.js in a debian-based container.
- The last parameter (npm) is the command you want to run in the container.
With this command at hand, you can now define a bash alias to always use the container explained above when executing NPM.
alias npm='docker run --rm -it -w /opt -v $(pwd):/opt node npm'
Put this line into your .bashrc or.bash_profile file and run source on it. Now you can safely uninstall NPM from your system.