-
-
Save jhazelwo/a6f635548c26c18f4f293c23eff4f4ad to your computer and use it in GitHub Desktop.
Docker script to aid with building and running containers
This file contains hidden or 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/sh | |
# | |
# Default Example | |
contname="" # contname="spaceship" | |
image_name="" # image_name="zbeeblebrox/${contname}:1.0" | |
nodename="" # nodename="--hostname=${contname}" | |
runname="" # runname="--name=${contname}" | |
run_rm="" # run_rm="--detach" | |
build_rm="" # build_rm="--force-rm=true" | |
port="" # port="-p `hostname -i`:80:80 -p `hostname -i`:443:443" | |
volume="" # volume="-v /var/log/containers:/app/log" | |
addhost="" # addhost="--add-host=docker.host:`hostname -i`" | |
startre="" # startre="--restart=unless-stopped" | |
with_tty="" # with_tty="--tty" | |
with_interact="" # with_interact="--interactive" | |
detach="" # detach="--detach" | |
build_context="$(dirname $0)" | |
tini="--init" # use /dev/init "Tiny Init" during `docker run` | |
# Require settings file | |
. ${build_context}/settings.sh || exit 1 | |
usage() { | |
echo "" | |
echo "$0 build [clean]" | |
echo "" | |
echo "$0 run" | |
echo "" | |
echo "$0 kill" | |
echo "" | |
echo "$0 rm [-f]" | |
echo "" | |
echo "$0 logs [-f] [recent]" | |
echo "" | |
} | |
do_build() { | |
echo "`date` docker build $build_rm --tag=${image_name} ${build_context}" | |
docker build $build_rm --tag=${image_name} ${build_context} | |
[ $? -eq 0 -a please_$1 = please_clean ] && { | |
for this in `/usr/bin/docker images |grep '<none>'|awk '{print $3}'`; do | |
/usr/bin/docker rmi $this | |
done | |
} | |
} | |
do_run() { | |
echo "`date` docker run $nodename $runname $run_rm $tini $port $detach $addhost $startre $volume $with_tty $with_interact $image_name $@" | |
docker run $nodename $runname $run_rm $tini $port $detach $addhost $startre $volume $with_tty $with_interact $image_name $@ | |
} | |
do_kill() { | |
echo "`date` docker kill $contname" | |
docker kill $contname | |
} | |
do_rm() { | |
if [ "$1" = "-f" ]; then force="-f"; else force=""; fi | |
echo "`date` docker rm $force $contname" | |
docker rm $force $contname | |
} | |
do_logs() { | |
follow="" | |
recent="" | |
if [ "$1" = "recent" -o "$2" = "recent" ]; then | |
recent="--since=`date --date='5 minutes ago' '+%s'`"; fi | |
if [ "$1" = "-f" -o "$2" = "-f" ]; then | |
follow="-f"; fi | |
echo "`date` docker logs $follow $recent $contname" | |
docker logs $follow $recent $contname | |
} | |
case "$1" in | |
build) | |
do_build "$2" | |
;; | |
run) | |
shift | |
do_run "$@" | |
;; | |
kill) | |
do_kill | |
;; | |
rm) | |
shift | |
do_rm "$@" | |
;; | |
logs) | |
shift | |
do_logs "$@" | |
;; | |
*) | |
usage | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment