Last active
November 5, 2016 12:22
-
-
Save cdpb/5e29e61b2ea6082655f6 to your computer and use it in GitHub Desktop.
Automatic Docker Build example
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/bash | |
set -e | |
while [[ $# > 0 ]] | |
do | |
case "$1" in | |
--folder) | |
shift | |
FOLDER="$1" | |
;; | |
--opt) | |
shift | |
OPT="$1" | |
;; | |
--tag) | |
shift | |
TAG="$1" | |
;; | |
--custom-docker-file) | |
shift | |
DOCKERFILE="$1" | |
;; | |
--repull) | |
REPULL="repull" | |
;; | |
--no-cache) | |
CACHE="--no-cache" | |
;; | |
--dry-run) | |
DRY="true" | |
;; | |
*|--help) | |
echo './make --tag localhost:5000/debian --folder /root/docker/debian --opt "git pull" --custom-docker-file Dockerfile.rpi --dry-run --repull --no-cache' | |
exit 0 | |
;; | |
esac | |
shift | |
done | |
if [[ -z $TAG ]]; then | |
echo 'check args -> ./make.sh --help' | |
exit 1 | |
fi | |
if [[ -z $OPT ]]; then | |
OPT="none" | |
fi | |
function build() { | |
TAGTMP=$TAG-tmp | |
TAGOLD=$TAG-old:$(date +%Y%m%d) | |
if [[ -z "${DOCKERFILE}" ]]; then | |
DOCKERFILE="Dockerfile" | |
fi | |
cd $FOLDER | |
BASE=$(cat $DOCKERFILE | grep -e 'FROM' | awk '{ print $2 }') | |
echo "start update $TAG ( base image $BASE ) in folder $FOLDER with options ' $OPT $REPULL'" | |
echo "start $(date)" | |
if [[ $DRY ]]; then | |
VERBOSE="echo" | |
else | |
VERBOSE="eval" | |
fi | |
if [[ "${OPT}" != "none" ]]; then | |
echo -n "$(pwd) -> " | |
$VERBOSE "${OPT}" | |
fi | |
# update base image | |
if [[ $REPULL ]]; then | |
$VERBOSE "docker pull $BASE" | |
fi | |
# build | |
$VERBOSE "docker build -t $TAGTMP -f $DOCKERFILE $CACHE ." | |
# retag existing image to -old | |
OLDC="$(docker images | awk '{print $1}' | grep ^$TAG\$ | wc -l )" | |
if [[ $OLDC > 0 ]]; then | |
$VERBOSE "docker tag $TAG $TAGOLD" | |
fi | |
# retag new image | |
$VERBOSE "docker tag $TAGTMP $TAG" | |
# untag tmp image | |
$VERBOSE "docker rmi $TAGTMP" | |
# push to registry | |
#$VERBOSE "docker push $TAG" | |
echo "stop $(date)" | |
} | |
FORMAT="$TAG_$(date +%F)_$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 6 | head -n 1)" | |
LOGPATH=/var/log/docker/build_$FORMAT.log | |
build | tee -a $LOGPATH | |
P="$?" | |
LOG=$(cat $LOGPATH) | |
if [[ $P -eq 0 ]]; then | |
SU="true" | |
else | |
SU="false" | |
fi | |
if [[ ! $DRY ]]; then | |
/mnt/docker/clean.sh | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment