实验目标:
client通过192.168.100.67访问到下列redis集群
容器ID为9cb25bcd52d1的ip地址:172.17.0.5 port: 7005 7006
容器ID为91dac3ea23c9的ip地址:172.17.0.4 port: 7003 7004
容器ID为e2189fc1d4d9的ip地址:172.17.0.2 port: 7001 7002
创建一个基础的rides镜像,包含基础包、ruby、redis接口
我们创建一个基础的rides镜像,这其中要安装必须的一些基础包,ruby等,Dockerfile如下,借用网上的稍作修改,本来打算把gem install redis一起在RUN命令中,但是连不到官方服务器,只能通过国外IP下载放到宿主机目录/root/redissoft作为共享卷让容器读取
[root@localhost CentOS7]# pwd
/root/centos7
[root@localhost centos7]# more Dockerfile
#
# MAINTAINER newjoyful@163.com
# Dockerizing CentOS7: Dockerfile for building CentOS images
#
FROM centos:centos7.1.1503
MAINTAINER newjoyful@163.com
ENV TZ "Asia/Shanghai"
ENV TERM xterm
ADD aliyun-mirror.repo /etc/yum.repos.d/CentOS-Base.repo
ADD aliyun-epel.repo /etc/yum.repos.d/epel.repo
VOLUME ["/root/redissoft"]
RUN yum install -y curl wget tar bzip2 unzip vim-enhanced passwd sudo yum-utils hostname net-tools rsync man && \
yum install -y gcc gcc-c++ git make automake cmake patch logrotate Python-devel libpng-devel libjpeg-devel && \
yum install -y --enablerepo=epel pwgen python-pip && \
yum install -y ruby rubygems && \
yum clean all
通过Dockfiler创建redis/centos:v7.1 镜像,并且安装redis接口,
[root@localhost centos7]# docker build -t redis/centos:v7.1 .
[root@localhost centos7]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis/centos v7.1 5812d7509345 About an hour ago 601.4 MB
现在有了基础镜像但是还没有安装gem install redis,所以我们进入容器通过gem install redis安装redis接口
[root@localhost ~]#docker run -it -v /root/redissoft:/root/redissoft redis/centos:v1
[root@9cb25bcd7895 ~]#cd /root/redissoft
[root@9cb25bcd7895 ~]#gem install redis-3.2.1.gem
[root@9cb25bcd7895 ~]#exit
创建redis/nodemodule:v1镜像
再创建镜像redis/nodemodule镜像用于redis节点容器的新建
[root@localhost ~]#docker commit -t 9cb25bcd7895 redis/nodemodule:v1
注:由于是实验并没有深究gem install redis-3.2.1.gem整合到Dockfile中,实际应用尽量将操作都要整合到Dockfile,其实以下所有操作都可以作为一个Dockfile来创建,后续再探索;
启动容器
[root@localhost centos7]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis/nodemodule v1 e5ea8f7834df 3 minutes ago 602.1 MB
#此镜像作为创建3个节点容器的模板,此镜像已经安装了ruby环境和基础环境包, 没有安装redis3.0.7;
[root@localhost ~]#docker run -itd -p 7001:7001 -p 7002:7002 -v /root/redissoft:/root/redissoft --name redisNodeA7000-70001 redis/nodemodule:v1 /bin/bash
[root@localhost ~]#docker run -itd -p 7003:7003 -p 7004:7004 -v /root/redissoft:/root/redissoft --name redisNodeA7003-70004 redis/nodemodule:v1 /bin/bash
[root@localhost ~]#docker run -itd -p 7005:7005 -p 7006:7006 -v /root/redissoft:/root/redissoft --name redisNodeA7005-70006 redis/nodemodule:v1 /bin/bash
[root@localhost ~]# docker ps #之所以用-v /root/redissoft:/root/redissoft参数是让容器能取到redis安装包
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2189fc1d4d9 redis/nodemodule:v1 "/bin/bash" 55 minutes ago Up 55 minutes 0.0.0.0:7001-7002->7001-7002/tcp redisNodeA7000-70001