#!/bin/bash if [ -z $1 ]; then echo "Usage: c run <image name>:<version> " echo " c stop <container name> " 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= 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
This article is from the "my operation and maintenance of the road" blog, declined reprint!
Docker container Restart script