Using NodeJS through docker as CLI

Sometimes you want to build or run a simple NodeJS or PHP script. But if your current OS doesn’t have Node, NPM, or Yarn installed it might be a hassle to install this. But if you have docker available you can run the node container as a CLI. Just add the docker run command as an alias to your host and use yarn or node as if it was living on your host itself.

alias yarn='docker run --rm -ti -v ${PWD}:/app -w="/app" node:lts-alpine yarn'
alias node='docker run --rm -ti -v ${PWD}:/app -w="/app "node:lts-alpine node'

We mount the current directory (${PWD}) to the /app directory in the container and make sure we remove (--rm) the container after running the commands.

By giving the -ti flag we make sure then when we can interact with the shell when needed, i.e. when running yarn init