Last active
February 4, 2017 22:58
-
-
Save marc-hanheide/d9b4bb6057665acf7524c7b79827f1c8 to your computer and use it in GitHub Desktop.
Some OSX docker magic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This file is to be "source-d" in your bash shell. | |
# Adapted from https://blog.bennycornelissen.nl/bwc-gui-apps-in-docker-on-osx/ | |
# then run as docker_run strands/strands-base for instance | |
# docker-machine stuff | |
if [ $(which docker-machine) ]; then | |
export C_DOCKER_MACHINE="docker-vm" | |
dminit() { | |
docker-machine start ${C_DOCKER_MACHINE} | |
dmshell | |
} | |
dmshell() { | |
eval $(docker-machine env ${C_DOCKER_MACHINE}) | |
} | |
docker_if_not_running() { | |
if [ $(docker-machine status ${C_DOCKER_MACHINE}) != 'Running' ]; then | |
dminit | |
fi | |
} | |
if [ $(docker-machine status ${C_DOCKER_MACHINE}) = 'Running' ]; then | |
dmshell &> /dev/null | |
fi | |
fi # end docker-machine | |
# Xquartz stuff | |
xquartz_if_not_running() { | |
v_nolisten_tcp=$(defaults read org.macosforge.xquartz.X11 nolisten_tcp) | |
v_xquartz_app=$(defaults read org.macosforge.xquartz.X11 app_to_run) | |
if [ $v_nolisten_tcp = "1" ]; then | |
defaults write org.macosforge.xquartz.X11 nolisten_tcp 0 | |
fi | |
if [ $v_xquartz_app != "/usr/bin/true" ]; then | |
defaults write org.macosforge.xquartz.X11 app_to_run /usr/bin/true | |
fi | |
netstat -an | grep 6000 &> /dev/null || open -a XQuartz | |
while ! netstat -an \| grep 6000 &> /dev/null; do | |
sleep 2 | |
done | |
export DISPLAY=:0 | |
} | |
docker_route_add() { | |
DOCKER_SUBNET=`docker network inspect bridge | grep '"Subnet"'| sed 's/.*Subnet.*"\([^"]*\)".*/\1/'` | |
DOCKER_MACHINE_IP="$(docker-machine ip ${C_DOCKER_MACHINE})" | |
sudo route add ${DOCKER_SUBNET} ${DOCKER_MACHINE_IP} | |
} | |
docker_route_remove() { | |
DOCKER_SUBNET=`docker network inspect bridge | grep '"Subnet"'| sed 's/.*Subnet.*"\([^"]*\)".*/\1/'` | |
DOCKER_MACHINE_IP="$(docker-machine ip ${C_DOCKER_MACHINE})" | |
sudo route delete ${DOCKER_SUBNET} ${DOCKER_MACHINE_IP} | |
} | |
docker_run() { | |
IMAGE="$1" | |
xquartz_if_not_running | |
docker_if_not_running | |
xhost +$(docker-machine ip ${C_DOCKER_MACHINE}) | |
docker run \ | |
-it \ | |
--rm \ | |
-e DISPLAY=$(docker-machine inspect ${C_DOCKER_MACHINE} --format={{.Driver.HostOnlyCIDR}} | cut -d'/' -f1):0 \ | |
"$IMAGE" /bin/bash | |
xhost -$(docker-machine ip ${C_DOCKER_MACHINE}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment