# 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地址记得修改。
docker主机创建完成。可以根据自己的项目安装软件了。当然真正要当测试环境使用时要注意哪些数据是固定不变的,哪些数据需要放到永久存储上的。装完软件后在宿主机执行./centos.sh commit 将固定不变的数据保存成image就ok了,第一次写,不知道表达清楚了没有。如有疑问可以与我qq交流,测试环境公司里已经用了一段时间,非常稳定,也非常方便、快捷。
博主QQ:568273240
博客:http://568273240.blog.51cto.com/