[Docker] Default Helper Scripts & files
Using Docker for test, it is annoying to write whole commands every time. For example, when I try to run an image, I should use long commands like following :
docker run --name="$HOST_NAME" -i -t --hostname="$HOST_NAME" -P --lxc-conf="lxc.network.type=veth" --lxc-conf="lxc.network.ipv4=${IP}/24" --lxc-conf="lxc.network.ipv4.gateway=172.17.42.1" --lxc-conf="lxc.network.link=docker0" --lxc-conf="lxc.network.name=eth0" --lxc-conf="lxc.network.flags=up" docker-registry:5000/rhel65-base:dns /bin/bash |
But what if I should run 6 images?.....I will be. ...
EVEN BEFORE starting test.
Never imagine!!
Therefore, I create some helper scripts and files :
These files are provided from github.
git clone https://github.com/Jooho/sample-docker.git |
Now you can see these kind of files :
Scripts (Based on dns server images)
1) bashrc
#Local IP => Set local IP variable. It will be used in container export IP=$(ifconfig eth0|grep -w inet|awk '{print $2}'|cut -d: -f2) #Change home folder cd /var/named |
2) docker-env.sh
#Default information to start this container export HOST_NAME="DNS" export IP="172.17.42.100" export IMAGE='docker-registry:5000/rhel65-base:dns' |
3) docker-build.sh
# docker build command helper script. . ./docker-env.sh docker build -t $IMAGE . |
4)Dockerfile
FROM docker-registry:5000/centos65:basic ADD epel-release-6-8.noarch.rpm /etc/yum.repos.d/ RUN rpm -ivh /etc/yum.repos.d/epel-release-6-8.noarch.rpm #For supervisor package RUN yum update -y RUN yum install -y java-1.7.0-openjdk RUN yum install -y unzip RUN yum install -y openssh-server RUN yum install -y openssh-clients RUN yum install -y supervisor RUN yum install -y bind ADD supervisord.conf /home/supervisord.conf RUN cat /home/supervisord.conf > /etc/supervisord.conf ADD bashrc /home/bashrc RUN cat /home/bashrc >> /etc/profile RUN echo 'root:redhat' | chpasswd ADD bind/named.conf /etc/ ADD bind/named.jhouse.zones /etc/ ADD bind/172.17.42.net /var/named/ ADD bind/10.64.160.net /var/named/ ADD bind/named.jhouse /var/named/ EXPOSE 22 8080 9990 9999 CMD ["/usr/bin/supervisord"] #CMD ["/bin/bash"] #CMD ["./start.sh"] |
This file will be changed for each image. Actually it is not helper file but I could be a practical Dockerfile example.
5)docker-start.sh
. ./docker-env.sh for iii in $1 $2 $3 do if ( echo $iii | grep "\-host" &> /dev/null ) then c_host=`echo $iii | awk -F "=" '{print $2}'` if [[ z$c_host == z ]]; then echo usage : -host=jboss1 exit 1 else HOST_NAME=$c_host fi fi if ( echo $iii | grep "\-ip" &> /dev/null ) then c_ip=`echo $iii | awk -F "=" '{print $2}'` if [[ z$c_ip == z ]]; then echo usage : -ip=172.17.42.20 exit 1 else IP=$c_ip fi fi if ( echo $iii | grep "\-cmd" &> /dev/null ) then c_cmd=`echo $iii | awk -F "=" '{print $2}'` if [[ z$c_cmd == z ]]; then echo usage : -cmd=/bin/bash exit 1 else CMD=$c_cmd fi fi done ## https://github.com/dotcloud/docker/issues/5857 # it is not compatible using -h and --net togather from docker version 1.0.0 docker run \ --name="$HOST_NAME" \ -i -t --hostname="$HOST_NAME" \ -P -n=false\ #-n will be replaced with --net=none --lxc-conf="lxc.network.type=veth" \ --lxc-conf="lxc.network.ipv4=${IP}/24" \ --lxc-conf="lxc.network.ipv4.gateway=172.17.42.1" \ --lxc-conf="lxc.network.link=docker0" \ --lxc-conf="lxc.network.name=eth0" \ --lxc-conf="lxc.network.flags=up" \ $IMAGE $CMD |
Usages :
# ./docker-start.sh (it will use default hostname from docker-env.sh) # ./docker-start.sh -host=hostname -ip=staticIP -cmd=executeCommand # ./docker-start.sh -host=mysql1 -ip=172.17.42.41 -cmd=/bin/bash |
6) docker-stop.sh
. ./docker-env.sh for iii in $1 $2 do if ( echo $iii | grep "\-host" &> /dev/null ) then c_host=`echo $iii | awk -F "=" '{print $2}'` if [[ z$c_host == z ]]; then echo usage : -host=jboss1 exit 1 else HOST_NAME=$c_host fi fi done echo "############### Running Docker Container ###############" docker ps echo "" echo "" echo "############### Stopped Docker Container ###############" docker ps -a export CONTAINER_NUM=`docker ps -a | grep -w $HOST_NAME |awk '{print $1}'` export RUNNING_CONTAINER_NUM=`docker ps |grep -w $HOST_NAME |awk '{print $1}'` for r in $RUNNING_CONTAINER_NUM;do echo docker stop $r docker stop $r done for c in $CONTAINER_NUM;do echo docker rm $c docker rm $c done
|
Usages :
# ./docker-stop.sh (it will use default hostname from docker-env.sh) # ./docker-stop.sh -host=hostname # ./docker-stop.sh -host=mysql1 |
7)suervisord.conf
[supervisord] nodaemon=true [program:sshd] command=/usr/sbin/sshd -D stdout_logfile=/var/log/supervisor/%(program_name)s.log stderr_logfile=/var/log/supervisor/%(program_name)s.log autorestart=true |
To execute several commands when a container start, supervisord util is useful. Hence, this package will be installed by default. Please refer supervisor home page for more detail :http://supervisord.org/