Last active
February 2, 2018 13:06
-
-
Save hanbei/9e5c9e5d1483a83c84f46b07f3841aa9 to your computer and use it in GitHub Desktop.
NPM docker local dev environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PWD=$(pwd) | |
docker run --rm -v $PWD:/opt/workspace -p 3000:3000 nodeenv "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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