本文共 1585 字,大约阅读时间需要 5 分钟。
#!/bin/bash if [ -z $1 ]; then echo "Usage: c run : " echo " c stop " exit 1 fi if [ -z $ETCD_HOST ]; then ETCD_HOST="192.168.2.98:4001" fi if [ -z $ETCD_PREFIX ]; then ETCD_PREFIX="app/servers" fi if [ -z $CPORT ]; then CPORT="80" fi if [ -z $FORREST_IP ]; then FORREST_IP=`ifconfig eth0| grep "inet addr" | head -1 | cut -d : -f2 | awk '{print $1}'` fi function launch_container { echo "Launching $1 on $FORREST_IP ..." CONTAINER_ID=`docker run -d --dns 172.17.42.1 -P -v /data:/data -v /etc/httpd/conf:/etc/httpd/conf -v /etc/httpd/conf.d:/etc/httpd/conf.d -v /etc/localtime:/etc/localtime:ro $1 /bin/sh -c "/usr/bin/supervisord -c /etc/supervisord.conf"` PORT=`docker inspect $CONTAINER_ID|grep "\"Ports\"" -A 50|grep "\"$CPORT/tcp\"" -A 3| grep HostPort|cut -d '"' -f4|head -1` NAME=`docker inspect $CONTAINER_ID | grep Name | cut -d '"' -f4 | sed "s/\///g"|sed -n 2p` echo "Announcing to $ETCD_HOST..." curl -XPUT "http://$ETCD_HOST/v2/keys/$ETCD_PREFIX/$NAME" -d value="$FORREST_IP:$PORT" echo "$1 running on Port $PORT with name $NAME" } function stop_container { echo "Stopping $1..." CONTAINER_ID=`docker ps -a| grep $1 | awk '{print $1}'` echo "Found container $CONTAINER_ID" docker stop $CONTAINER_ID echo http://$ETCD_HOST/v2/keys/$ETCD_PREFIX/$1 curl -XDELETE http://$ETCD_HOST/v2/keys/$ETCD_PREFIX/$1 &> /dev/null echo "Stopped." } if [ $1 = "run" ]; then launch_container $2 else stop_container $2 fi
转载于:https://blog.51cto.com/linuxpython/1625974