Skip to content

Instantly share code, notes, and snippets.

@reitzig
Last active June 15, 2020 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reitzig/3b41025ed3d3ebc12e49fa692a6055da to your computer and use it in GitHub Desktop.
Save reitzig/3b41025ed3d3ebc12e49fa692a6055da to your computer and use it in GitHub Desktop.
Docker-based wrapper for npm and friends
#!/usr/bin/env bash
# Usage:
#
# - Run something: ./$ npm --version
# - Setup something: Add a suitable `prepare` script to your `package.json`, e.g.
#
# "scripts": {
# "prepare": "npm i -g @zeit/ncc"
# }
#
# Important: This container jumps into your work directory with root permissions!
set -eu
project_name="$(basename "${PWD}")"
container_name="${project_name}-node-box"
container_exists() {
docker ps --all --format 'table {{.Names}}' | grep -qc "${1}"
}
if ! container_exists "${container_name}"; then
docker run \
--detach --rm \
--name "${container_name}" \
-v "$PWD":/usr/src/app \
-w /usr/src/app \
node:12-slim \
tail -f /dev/null # block
echo "Container ${container_name} started -- don't forget to stop it later!"
docker exec -it \
-w /usr/src/app \
"${container_name}" \
npm run prepare
fi
docker exec -it \
-w /usr/src/app \
"${container_name}" \
"${@}"
@reitzig
Copy link
Author

reitzig commented Jun 15, 2020

"Proper" implementation based on this idea: container-do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment