TIL

Quick development environments with Docker

I often want to test something on a specific version of Python or some other tool. For more "permanent" environments, I use asdf. But for quick tests, I use Docker via this bash function:

function dr {
  local image=$1
  shift

  # If no additional arguments were provided, use the default command "bash -l"
  if [ $# -eq 0 ]; then
    set -- bash -l
  fi

  docker run --rm -it -v "$(pwd):/app" --workdir /app "$image" "$@"
}

This will open a bash shell in the image you specify with the current directory mounted as /app and drop you into that directory. You can also specify a command to run instead of bash -l with an optional second argument. It will also delete the container when you exit to avoid using up all your disk space.

For example, to get a quick shell on Python 3.11:

$ dr python:3.11

Or to test a Node script on Node 16:

$ dr node:16 npm run test