内存:16G
cpu: i3
ubuntu 14.10
docker 1.4.1
这里要提一下,docker最好升级到1.3.2以后,不然没法用到docker的国内镜像
升级方法: sudo curl -sSL https://get.docker.com/ubuntu/ | sh
# vim /etc/default/docker
末尾加上
DOCKER_OPTS="$DOCKER_OPTS --registry-mirror='http://xxxxxx.m.daocloud.io' -d"
参考资料: http://www.oschina.net/news/57894/daocloud
二 用Docerfile的方法构建自己的映像
# docker pull centos:6.6
# vim Dockerfile #安装基本的系统工具还有基本的服务,并实现开机启动
#Dockerfile
FROM centos:6.6
MAINTAINER mageguoshi <mageguoshi@126.com>
# install epel
RUN rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# install tools
RUN yum install -y wget lrzsz mlocate ntp ntpdate vim tar supervisor lsof iftop telnet
# RUN yum install -y salt salt-minion
# install software
RUN yum install -y openssh-server openssh-clients openssh
RUN yum install -y httpd
RUN yum install -y mysql-server mysql
RUN ssh-keygen -q -N "" -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
RUN echo 'root:123456' | chpasswd
# 在Dockerfile里无法启动服务,所以下面注释的3行是错误的
#RUN /etc/init.d/mysqld start
#RUN mysqladmin -uroot password 123456
#RUN mysql -uroot -p123456 -e "grant all privileges on *.* to 'test'@'%' identified by '123456'"
RUN mysql_install_db --user=mysql
#安装jdk1.7
COPY jdk-7u71-linux-x64.tar.gz /root/
RUN tar zxf /root/jdk-7u71-linux-x64.tar.gz -C /usr/local
RUN rm -f /root/jdk-7u71-linux-x64.tar.gz
RUN ln -s /usr/local/jdk1.7.0_71 /usr/local/jdk
RUN echo 'JAVA_HOME=/usr/local/jdk' >> /etc/profile
RUN echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
RUN echo 'export JAVA_HOME PATH' >> /etc/profile
RUN source /etc/profile
RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisord.conf
EXPOSE 22
CMD ["/usr/bin/supervisord"]
三 用固定ip的方式创建容器
参考资料: http://dockerone.com/question/50
我自己写了简单的脚本来创建容易,并分配主机名和ip
# vim create_docker_container.sh
#!/bin/sh
br_name=docker_gr
interface=eth1
gateway='192.168.1.1'
host_ip='192.168.1.18'
docker_gr=`brctl show | grep docker_gr |wc -l`
if [ $docker_gr -eq 0 ];then
brctl addbr $br_name # 创建一个网桥
ip addr add $host_ip/24 dev $br_name # 给网桥分配一个ip
ip addr del $host_ip/24 dev $interface # 把物理网卡的ip取消掉
ip link set $br_name up # 激活网桥
brctl addif $br_name $interface # 把物理网卡搭在网桥上
route add default gw $gateway # 添加默认网关
fi
# start new container
hostname=$1
container_ip=$2
if [ -z $hostname ];then
echo 'error: hostname can not be null'
echo "useage: ./$0 hostname ip"
exit
fi
if [ -z $container_ip ];then
echo 'error: container_ip can not be null'
echo "useage: ./$0 hostname ip"
exit
fi
bool=`ping $container_ip -c 2 |grep '100% packet loss'`
if [ $? -eq 1 ];then
echo 'error: container_ip have been exists'
exit
fi
cid=$(docker run -d -i -h=$hostname --name=$hostname --net=none -t mageguoshi/centos6-ssh)
pid=$(docker inspect -f '{{.State.Pid}}' $cid)
# set up netns
mkdir -p /var/run/netns
ln -s /proc/$pid/ns/net /var/run/netns/$pid # 绑定进程的网络命名空间软链
# set up bridge
ip link add q$pid type veth peer name r$pid # 在内核里创建一对虚拟网卡
brctl addif $br_name q$pid # 把第一个块虚拟网卡搭在网桥上
ip link set q$pid up # 启动第一块虚拟网卡
# set up docker interface
fixed_ip="$container_ip/24"
ip link set r$pid netns $pid # 把第二块网卡放到网络命名空间
ip netns exec $pid ip link set dev r$pid name $interface # 在命名空间了把第二块网卡重命名成xxx
ip netns exec $pid ip link set $interface up # 激活第二块网卡
ip netns exec $pid ip addr add $fixed_ip dev $interface # 给第二块网卡分配IP
ip netns exec $pid ip route add default via $gateway # 给第二块网卡设置默认网关
四 容器关闭后的启动脚本,现在分配和宿主机同一网段的ip还是比较麻烦的
# vim start_docker_container.sh
#!/bin/sh
br_name=docker_gr
interface=eth1
gateway='192.168.1.1'
host_ip='192.168.1.18'
docker_gr=`brctl show | grep docker_gr |wc -l`
if [ $docker_gr -eq 0 ];then
brctl addbr $br_name
ip addr add $host_ip/24 dev $br_name
ip addr del $host_ip/24 dev $interface
ip link set $br_name up
brctl addif $br_name $interface
route add default gw $gateway
fi
# start new container
hostname=$1
container_ip=$2
if [ -z $hostname ];then
echo 'error: hostname can not be null'
echo "useage: ./$0 hostname"
exit
fi
if [ -z $container_ip ];then
echo 'error: container_ip can not be null'
echo "useage: ./$0 hostname ip"
exit
fi
bool=`ping $container_ip -c 2 |grep '100% packet loss'`
if [ $? -eq 1 ];then
echo 'error: container_ip have been exists'
exit
fi
cid=$(docker start $hostname)
pid=$(docker inspect -f '{{.State.Pid}}' $cid)
# set up netns
mkdir -p /var/run/netns
ln -s /proc/$pid/ns/net /var/run/netns/$pid
# set up bridge
ip link add q$pid type veth peer name r$pid
brctl addif $br_name q$pid
ip link set q$pid up
# set up docker interface
fixed_ip="$container_ip/24"
ip link set r$pid netns $pid
ip netns exec $pid ip link set dev r$pid name $interface
ip netns exec $pid ip link set $interface up
ip netns exec $pid ip addr add $fixed_ip dev $interface
ip netns exec $pid ip route add default via $gateway
# sh start_docker_container.sh my_hostname 192.168.1.11