Skip to content

Instantly share code, notes, and snippets.

@hanbei
Last active February 2, 2018 13:06
Show Gist options
  • Save hanbei/9e5c9e5d1483a83c84f46b07f3841aa9 to your computer and use it in GitHub Desktop.
Save hanbei/9e5c9e5d1483a83c84f46b07f3841aa9 to your computer and use it in GitHub Desktop.
NPM docker local dev environment
PWD=$(pwd)
docker run --rm -v $PWD:/opt/workspace -p 3000:3000 nodeenv "$@"
FROM node:8.9.4-alpine
RUN apk update
RUN apk upgrade
RUN apk add bash
RUN mkdir -p /opt/workspace
WORKDIR /opt/workspace
VOLUME /opt/workspace
COPY entrypoint.sh /entrypoint.sh
RUN chmod 755 /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]
CMD [ "npm", "start" ]
EXPOSE 3000
#!/usr/bin/env bash
set -e
set -o pipefail
# calculate the md5 sum of the package.json and save it in the node_module directory
function calc_package_md5 {
md5sum ./package.json | awk '{print $1}' > ./node_modules/package_json_md5
}
# install npm dependencies
function npmi {
npm prune
npm install
npm ddp # npm dedupe - flatten node_modules hierarchy
}
# install / update dependencies only if necessary
function prepare {
# ok, is there a node_modules folder?
if [[ ! -d './node_modules' ]]; then
npmi
calc_package_md5
return
fi
# ok, node_modules folder there, but is there an old package_json_md5 file?
if [[ ! -f './node_modules/package_json_md5' ]]; then
npmi
calc_package_md5
return
fi
# ok all is there, but did the package json update?
if [[ "$(md5sum ./package.json | awk '{print $1}')" != "$(cat ./node_modules/package_json_md5)" ]]; then
npmi
calc_package_md5
return
fi
}
# install / update dependencies if necessary
prepare
# run the actual command given
# - use double quotes to prevent splitting of arguments with spaces
exec npm "$@"
PWD=$(pwd)
docker run --rm -v $PWD:/opt/workspace -p 3000:3000 nodeenv "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment