Skip to content

Instantly share code, notes, and snippets.

@mrceperka
Last active May 4, 2018 10:24
Show Gist options
  • Save mrceperka/a32c14dd86ebffc31a8dcf7b15aa8308 to your computer and use it in GitHub Desktop.
Save mrceperka/a32c14dd86ebffc31a8dcf7b15aa8308 to your computer and use it in GitHub Desktop.
Docker bash aliases
#!/bin/bash
function get_free_port() {
python -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()'
}
function get_pwd_md5() {
python -c 'import hashlib; print hashlib.md5("'`pwd`'").hexdigest()'
}
function docker_alias_core() {
local APP_DIR=$1
shift
local PORT=$1
shift
docker run \
-it \
--rm \
--user $(id -u):$(id -g) \
-v $(pwd):$APP_DIR \
-w $APP_DIR \
-p $PORT:3000 \
$*
}
function docker_alias_net_host() {
local APP_DIR="/app_$(get_pwd_md5)"
docker run \
-it \
--rm \
--user $(id -u):$(id -g) \
-v $(pwd):$APP_DIR \
-v /home/mrceperka/.ssh:/.ssh_host:ro \
-w $APP_DIR \
--net host \
$*
}
function docker_alias_no_port() {
local APP_DIR="/app_$(get_pwd_md5)"
docker run \
-it \
--rm \
--user $(id -u):$(id -g) \
-v $(pwd):$APP_DIR \
-w $APP_DIR \
$*
}
function docker_alias_open_browser() {
echo $*
local PORT=`get_free_port`
local APP_DIR="/app_$(get_pwd_md5)"
echo "App will run at port: $PORT"
echo "App dir: $APP_DIR";
open_browser $PORT
docker_alias_core $APP_DIR $PORT $*
}
function open_browser() {
xdg-open http://localhost:$1
}
alias npm="docker_alias_open_browser node npm $*"
alias yarn="docker_alias_open_browser node yarn $*"
alias node="docker_alias_open_browser node $*"
alias node_host="docker_alias_net_host node $*"
alias php="docker_alias_no_port php $*"
alias php_host="docker_alias_net_host php $*"
alias wg_php="docker_alias_net_host webgarden/php:7.1 $*"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment