centos+bond+bridge+docker(ssh容器)固定ip實現測試環境(四)

# cat Dockerfile

# test/centos:ssh

#

# VERSION               0.0.1

 

FROM        docker.io/centos 

MAINTAINER  test "[email protected]"

 

RUN         yum install -y openssh openssh-server openssh-clients

RUN         mkdir /var/run/sshd

RUN         ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

RUN         ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key

RUN         ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key

RUN         /bin/echo 'root:test2015' |chpasswd

RUN         /bin/sed -i 's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g' /etc/pam.d/sshd

RUN         /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local

RUN         /usr/bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

EXPOSE      22

CMD    /usr/sbin/sshd -D


# docker build -t ycy/centos:aaa .

啓動腳本:centos.sh

#!/bin/bash

#CONTID=`docker ps  -q -l`

repo=ycy/centos:aaa

if [ $# -ne 1 ]; then

        echo $0 '[start|stop|restart] ' 

        exit

fi

getCONTID (){

        CONTID=`docker ps | grep ${repo}  | awk '{print $1}'`

}


stoprepo () {

        getCONTID

        if [ -z $CONTID ] ; then echo "already stop $repo ...";exit;  fi

        docker stop $CONTID

cleanip

}

#getCONTID

#if [ $1 == stop ] ; then stoprepo; exit ;fi


chkrepo () {

getCONTID

        if [ ! -z $CONTID  ] ; then echo "already start $repo ..." ; exit ; fi

        }

startrepo () {

docker run -d --privileged -v /opt:/opt  --net=none ${repo}

}


networkrepo () {

        getCONTID

pid=`docker inspect -f '``.`State`.`Pid`' $CONTID`

if [ $pid -eq 0 ] ; then echo "err exit" ; exit ; fi

mkdir -p /var/run/netns

ln -s /proc/$pid/ns/net /var/run/netns/$pid

ip link add ${pid}A type veth peer name ${pid}B

brctl addif bridge0 ${pid}A

ip link set ${pid}A up

ip link set ${pid}B netns $pid

ip netns exec $pid ip link set dev ${pid}B name eth0 address aa:af:ac:d3:53:b2

ip netns exec $pid ip link set eth0 up

ip netns exec $pid ip addr add 192.168.1.3/24 dev eth0

ip netns exec $pid ip route add default via 192.168.1.1

echo "finish exit"

}

cleanip () {

for  i in `ip netns` 

do

ps -eo pid | grep $i > /dev/null

        if [ $? -eq 1 ]

then ip netns dele $i

fi

done

}

commitrepo () {

        getCONTID

        if [ -z $CONTID ] ; then echo "already stop $repo ...";exit;  fi

        docker commit $CONTID $repo

}

case $1 in

start)

cleanip

        chkrepo

        startrepo $repo

        networkrepo $CONTID

        ;;

stop)

        stoprepo

        ;;

restart)

        getCONTID

        if [ ! -z $CONTID ] ;then stoprepo ; fi;

cleanip

        startrepo $repo

        networkrepo $CONTID

        ;;

commit)

commitrepo

;;

*)

        echo $0 '[start|stop|restart] '

        ;;

esac


腳本是我自己寫的,比較粗糙,讓大家見笑了。

# ./centos.sh start|stop|restart

不解釋了,地球人都知道。

注意,腳本的IP和MAC地址記得修改。

wKiom1XlQiaCUtUiAARvwXljdeI326.jpg

docker主機創建完成。可以根據自己的項目安裝軟件了。當然真正要當測試環境使用時要注意哪些數據是固定不變的,哪些數據需要放到永久存儲上的。裝完軟件後在宿主機執行./centos.sh commit 將固定不變的數據保存成p_w_picpath就ok了,第一次寫,不知道表達清楚了沒有。如有疑問可以與我qq交流,測試環境公司裏已經用了一段時間,非常穩定,也非常方便、快捷。

    博主QQ:568273240

    博客:http://568273240.blog.51cto.com/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章