Create NPM Developer environment in Docker

Docker benefits of course having a container without affecting your system installs.

In short. Containerized environment using Docker, in this case with NPM.

Get Docker
Check this github

Setup

Create working directy

mkdir npm-working-env && cd npm-working-env
mkdir node_modules

file: Dockerfile

FROM mhart/alpine-node

ENV HOME=/home/node  
ENV NODE_WORKDIR=$HOME

RUN apk --update add \  
 git \
 bash \
 python \
 openssl \
 libgcc \
 make \
 libstdc++ \
 g++ \
 openssh-client

RUN rm -rf /var/cache/apk/*

RUN addgroup node \  
 && adduser -h /home/node -s /bin/false -G node -D node

ADD ./node_modules $NODE_WORKDIR/  
RUN chown -R node:node $HOME 

USER node  
WORKDIR $NODE_WORKDIR

ENTRYPOINT /bin/sh 

file: docker-compose.yml

version: '2'

services:  
  npm:
    build: .
    environment:
      NODE_WORKDIR: /home/node
    volumes:
      - ./:/home/node
      - ./node_modules:/home/node/node_modules

Run

Run Docker Container

# build
docker-compose build
# start up
docker-compose up

You’ll see something like this

$ docker-compose up
Creating network "npmworkingenv_default" with the default driver
Creating npmworkingenv_npm_1
Attaching to npmworkingenv_npm_1
npmworkingenv_npm_1 exited with code 0

Start up the shell

Before we run the shell let's check
if it's already running

docker-compose ps
  Name                     Command         State    Ports 
---------------------------------------------------------
npmworkingenv_npm_1   /bin/sh -c /bin/sh   Exit 0         

Then we start up the shell

docker-compose run npm

the npm are from docker-compose.yml service name

And you’ll see the shell prompt

~ $ 

Inside the shell

cd node_modules

# create a package name
mkdir flip-test-app
cd flip-test-app

# login npm
npm login

result

Username: harianto
Password: 
Email: (this IS public) hariantoatwork@gmail.com
Logged in as harianto on https://registry.npmjs.org/.

For how to publish NPM packages
look here

Other options

# stop
docker-compose down

# start as daemon
docker-compose start

# stop as daemon
docker-compose stop

Remove dangling docker images

docker rmi -f `docker images -q --filter "dangling=true"`

To check running container: docker-compose ps