自动创建docker容器脚本

#!/bin/bash -xv
#set -x
HOST_NAME=$1
GATEWAY=172.27.0.1
#ISO_PATH=/root/iso
#VOLUME_PATH=/home/volume/$HOST_NAME
image=4paradigm/centos:7.2


function getIp {
count=1
while [ $count -lt 128 ]
do
HOST_IP=172.27.4.${count}
ping -c 1 $HOST_IP >/dev/null
if [[ $? -eq 0  ]];then
count=`expr $count + 1`
continue
else
break
fi
done
echo $HOST_IP
}


HOST_IP=$(getIp)
echo "----------------------"
echo "container IP : $HOST_IP"


if [[ $# != 1 ]];then
echo "use command create_docker.sh hostname to exec shell "
exit
fi


if [[ `docker ps | grep $HOST_NAME | wc -l` != 0 ]];then
echo "echo containor name has been used"
exit
elif [[ `docker ps -a | awk '{print $NF}'  | grep $HOST_NAME | wc -l` != 0  ]];then
echo "echo containor exist,but status is stop"
docker start $HOST_NAME
echo "----------------------------"
echo "pipework br0 $HOST_NAME ${HOST_IP}/20@$GATEWAY"
pipework br0 $HOST_NAME ${HOST_IP}/20@$GATEWAY
docker exec -d $HOST_NAME /usr/sbin/sshd -D
if [[ $? == 0  ]];then
echo "containor $HOST_NAME start successful"
fi
fi


if [ `docker ps -a | awk '{print $NF}'  | grep  $HOST_NAME | wc -l` -eq 0 ];then
    docker run -itd --privileged -h $HOST_NAME --name=$HOST_NAME --net=none  $image /usr/sbin/init
    pipework br0 $HOST_NAME $HOST_IP/20@$GATEWAY
    docker exec -d $HOST_NAME /usr/sbin/sshd -D
if [[ $? == 0  ]];then
                echo "containor $HOST_NAME start successful"
        fi
fi


你可能感兴趣的:(linux,docker)