Skip to content

Instantly share code, notes, and snippets.

@erichiggins
Last active December 4, 2015 03:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erichiggins/a8bcd07295597d91dea7 to your computer and use it in GitHub Desktop.
Save erichiggins/a8bcd07295597d91dea7 to your computer and use it in GitHub Desktop.
Enables command deligation to Docker images for a specific repo or directory.
# https://gist.github.com/erichiggins/a8bcd07295597d91dea7/
# version: 0.1.0
# author: Eric Higgins <erichiggins@gmail.com>
#
# Installation:
#
# 1) Copy this file into your repo's directory.
# cp docker_passthru.bash.inc ~/src/myrepo/
# 2) Add the following to your ~/.bash_profile to include this script.
# source ~/src/myrepo/docker_passthru.bash.inc
# 3) Modify as needed for the commands you want to support and your Docker images (See below).
#
# Usage:
#
# 1) Enter your repo working directory.
# cd ~/src/myrepo/
# 2) Run commands as you normally would.
# python test.py
# python serve.py
# gulp build
# gulp test
# gulp serve
# Uncomment the command aliases you want to use.
activate() {
alias python=_python
# alias ruby=_ruby
# alias rake=_rake
# alias gulp=_gulp
# alias grunt=_grunt
# alias node=_node
# alias npm=_npm
}
deactivate() {
unalias python ruby rake gulp grunt node npm &> /dev/null
}
# Watches for directory changes.
cd() { builtin cd "$@" && chpwd; }
# Path of this directory.
export CODE_PATH=`pwd -P`
# Modify the following with arguments needed for running python commands in a Docker image.
_python() {
(set -x; docker run -d -a stdout -a stderr --rm YOURPYTHONIMAGE python "$@")
}
_ruby() {
(set -x; ruby "$@")
}
_rake() {
(set -x; rake "$@")
}
_gulp() {
(set -x; docker run -d -a stdout -a stderr --rm YOUR_GULP_IMAGE gulp "$@")
}
_grunt() {
(set -x; grunt "$@")
}
_node() {
(set -x; node "$@")
}
_npm() {
(set -x; npm "$@")
}
# Run `activate` when inside of `CODE_PATH`, else run `deactivate`.
chpwd () {
case $PWD in
$CODE_PATH|$CODE_PATH/*)
activate
;;
*)
deactivate
;;
esac
}
# Run on init.
chpwd
@jsm
Copy link

jsm commented Oct 28, 2015

how would this work with running a script?

_python blah.py

@erichiggins
Copy link
Author

@jsm You still just run python blah.py from your terminal like you normally would. This script changes it from python blah.py to docker run python27img python blah.py to run the same command within the confines of the Docker image then dumps the output back to your terminal.

@erichiggins
Copy link
Author

Open questions:

  • How would something like gulp watch work?
  • If you change requirements.txt or package.json, how will the Docker image know to pick it up?
  • How are Docker images managed (new versions added, deployed)?

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