├── zsh-docker-run.plugin.zsh └── README.md /zsh-docker-run.plugin.zsh: -------------------------------------------------------------------------------- 1 | function can_be_run_through_docker_compose_service() { 2 | # Look for a service using the image $1 inside docker-compose.yml 3 | image_name='' 4 | if [ -f "docker-compose.yml" ]; 5 | then 6 | image_name=$(grep -B1 -A0 "image: $1" docker-compose.yml | head -n1 | awk -F ":" '{print $1}' | tr -d '[:space:]') 7 | fi 8 | } 9 | 10 | function docker_run() { 11 | docker run --rm -it -u $UID -v $PWD:/sandbox -v $HOME:$HOME -e HOME=$HOME -w /sandbox --entrypoint=$3 $1:$2 ${@:4} 12 | } 13 | 14 | function docker_compose_run() { 15 | docker-compose run --rm --entrypoint=$1 ${@:2} 16 | } 17 | 18 | function run_with_docker() { 19 | can_be_run_through_docker_compose_service $1 20 | 21 | if [[ ! -z "${image_name// }" ]]; 22 | then 23 | docker_compose_run $3 $image_name ${@:4} 24 | else 25 | docker_run $1 $2 $3 ${@:4} 26 | fi 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zsh-docker-run 2 | 3 | ## Run commands naturally, forgetting all about Docker 4 | 5 | Run commands "naturally" without having them installed, using Docker. 6 | 7 | ### Why? 8 | 9 | As we adopt Docker more, we install a whole lot less on our machines. Instead of installing node, php, ruby - we pull their latest Docker images. This means that simple commands, such as `npm install` and `irb` become more cumbersome, such as: 10 | 11 | ```shell 12 | docker run --rm -it --entrypoint=irb ruby:latest 13 | ``` 14 | 15 | It'd be much better if we could work as we used to, keeping our simple commands. Your first thought might be "use an alias" ... but we want to be better than that. What if we're inside of a project that's using `docker-compose` and we want to update our node packages? 16 | 17 | Normally, this is just `npm update`; but instead, we now need to use `docker-compose run --rm node npm update` 18 | 19 | *Using this plugin, we can detect these situations for you*. Go back to using `npm update`. Just add a simple function and call `run_with_docker` :smile: 20 | 21 | ### How? 22 | 23 | Install this plugin with your favourite zsh plugin manager, I use `zplug`: 24 | 25 | ```shell 26 | # For zplug 27 | zplug "rawkode/zsh-docker-run" 28 | ``` 29 | 30 | Now all you need to-do is add some functions to your `.zshrc` file: 31 | 32 | ```shell 33 | function example { 34 | run_with_docker "image_name" "image_tag" "command" $@ 35 | } 36 | 37 | function npm { 38 | run_with_docker "node" "alpine" "npm" $@ 39 | } 40 | ``` 41 | 42 | ### More Examples 43 | 44 | ```shell 45 | function elixir() { 46 | run_with_docker "elixir" "latest" "elixir" $@ 47 | } 48 | 49 | function iex() { 50 | run_with_docker "elixir" "latest" "iex" $@ 51 | } 52 | 53 | function mix() { 54 | run_with_docker "elixir" "latest" "mix" $@ 55 | } 56 | 57 | function go() { 58 | run_with_docker "golang" "latest" "go" $@ 59 | } 60 | 61 | function php() { 62 | run_with_docker "php" "latest" "php" $@ 63 | } 64 | ``` 65 | 66 | ### What's Next? 67 | 68 | - [ ] I'd like to provide a simpler interface, replacing functions with a map? 69 | - [ ] Possibly provide a bunch of defaults, enabling the more popular commands 70 | 71 | --------------------------------------------------------------------------------