-
-
Save reitzig/3b41025ed3d3ebc12e49fa692a6055da to your computer and use it in GitHub Desktop.
Docker-based wrapper for npm and friends
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 | |
# 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}" \ | |
"${@}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"Proper" implementation based on this idea: container-do