Docker testing without compose

Sometimes using docker-compose during testing can be a hassle. Use the following little magic to spawn and test multiple instances.
Your mileage will vary.

#!/bin/bash

#
# A semi automated way to launch multiple docker containers and test your apps parallelization
# replace the creation of index.php with your favorite git cloned code
# angelos@unix.gr
#

DOCKER=”sudo docker”
IMAGE=worker
DOCKERHOST=localhost:3000

function build {
echo ‘ ‘ > index.php

echo ‘
FROM centos:centos6
EXPOSE 80
RUN yum -y update && \
yum -y install epel-release && \
yum -y install mod_php
ENTRYPOINT [“/usr/sbin/apachectl”, “-D”, “FOREGROUND”]
‘ > Dockerfile

echo “Building Master Image”
$DOCKER build . 2>&1 | tee build.log
id=`grep ‘Successfully built’ build.log | cut -d” ” -f 3`
if [ “X${id}” == “X” ]
then
echo “build failed”
exit 1
fi

$DOCKER tag -f ${id} $IMAGE
}

function runem {
for instance in `seq 1 $1`
do
# might give an error
$DOCKER rm worker-instance${instance} >& /dev/null

hash=`$DOCKER run -d \
-p $((80+${instance})):80 \
-h worker-instance${instance} –name=worker-instance${instance} \
$IMAGE`
done

echo “======================= Docker Images ======================”
$DOCKER ps
}

function killem {
INSTANCES=`$DOCKER ps | grep worker-instance | awk ‘{print $11}’`
for instance in $INSTANCES
do
$DOCKER kill ${instance} && $DOCKER rm ${instance}
done
}

function checkem {
INSTANCES=`$DOCKER ps| grep worker-instance | wc -l`
if [ $INSTANCES -le 0 ]
then
echo “[ERROR] No Instances found ”
exit 1
fi
> usage.txt
> processes.txt
for instance in `seq 1 $INSTANCES`
do
curl -s http://localhost:$((80+${instance}))| grep worker-instance${instance} >& /dev/null
if [ $? -ne 0 ]
then
echo “Instance ${instance} is not healthy”
else
echo “Instance ${instance} is fine”
fi

echo worker-instance${instance} >> processes.txt
$DOCKER exec worker-instance${instance} ps aux >> processes.txt 2>&1

echo worker-instance${instance} >> usage.txt
$DOCKER exec worker-instance${instance} w >> usage.txt 2>&1
done

echo Process list is in processes.txt , mem/cpu usage in usage.txt
}

function remote {
curl -s http://localhost:2376/containers/worker-instance${1}/stats?stream=false | sed -e ‘s/[{}]/”/g’ | awk -v RS=’,”‘ -F: ‘{print $1 ” ” $2}’ | sed -e ‘s/\”//g’
}

function usage {
echo ”
Usage:
[build image] ./$0 -b
[run image instances] ./$0 -r
[delete image] ./$0 -d
[kill running instances] ./$0 -k
[check instances] ./$0 -c
[check instance via remote api] ./$0 -a

}

while getopts “:r:a:cbk” opt; do
echo “$opt was triggered, Parameter: $OPTARG” >&2
case $opt in
a)
remote $OPTARG
;;
d)
docker rmi -f $IMAGE
;;
k)
echo “Killing Instances”
killem
;;
r)
runem $OPTARG
;;
b)
build
;;
c)
checkem
;;
\?)
usage
exit 1
;;
šŸ™‚
echo “Option -$OPTARG requires an argument.” >&2
exit 1
;;
esac
done

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: