Private NPM Module with Docker Containers

NPM had announced support for private modules and now its possible to have private or public NPM package which you can publish to Github, Gitlab or NPM repository.
what is NPM Package —
[npm
Edit descriptionwww.npmjs.com](https://www.npmjs.com/ "https://www.npmjs.com/")
If we talk about NPM which is node package manager but now its like javascript package manager where you can publish a package private or public and your other apps can use that package.
Package is nothing but some software utility or some sdk interface, such package we can get and those feature will get added in our JavaScript application like webpack, grunt, gulp
to add these package or library if we can install simply
npm install — save pkg-name
Can we build such javascript package and publish it, Yes we can publish such packages and others can consume these packages.
Now how to work with Private package which are limited to your organization or can be access with valid access or auth tokens.
Out point of discussion here is how to work with private packaged using Docker or Locally
If we are running application locally and we need to install some private or Organization package then we need to configure our .npmrc file
vi ~/.npmrc
always-auth=true
//gitlab.com/api/v4/packages/npm/:_authToken=AUTH_TOKEN
//gitlab.com/api/v4/projects/PROJECT_ID/packages/npm/:_authToken=AUTH_TOKEN
[@](http://twitter.com/zr "Twitter profile for @zr")my_org:registry=https://gitlab.com/api/v4/packages/npm/
Here an example for .npmrc for gitlab private repository, here we have to specify AUTH_TOKEN which we can get from account -> settings-> access-token
This .npmrc will help us to pull private packages which are published in private scope as we have valid credentials to fetch private package
npm install —- save @my_org/packagename
We can run npm install and it will install all public and private package as we have .npmrc which will validate package pull from private repository
Now what if we have to do same for Docker setup, In this case we have to make few arrangements Dockerfile for your Node projects
To access the private modules in NPM, we need to pass the GIT_TOKEN environment variable to the Docker image.
ENV GIT_TOKEN=token
The naive approach would be to add it using the ENV, However, it does not work. The variables set with ENV are for runtime only. so we can have setup like below mentioned Dockerfile
You can test docker build for this container by passing GIT_TOKEN argument during build, To build the image using this image and the token, you can run Docker:
docker build --build-arg GIT_TOKEN=${GIT_TOKEN} .
Now we can update docker-compose.yml and populate GIT_TOKEN for our build environment
docker-compose.override.yml
.npmrc in your code base can look like this which will map to container .npmrc using volume mapping
always-auth=true
//gitlab.com/api/v4/packages/npm/:_authToken=AUTH_TOKEN
//gitlab.com/api/v4/projects/PROJECT_ID/packages/npm/:_authToken=AUTH_TOKEN
[@](http://twitter.com/zr "Twitter profile for @zr")my_org:registry=https://gitlab.com/api/v4/packages/npm/


