十、手动将容器保存为镜像

10.1 要求:制作一个只有ssh服务的镜像

10.1.1 启动一个容器

docker run -it -p 85:80 centos:6.9
yum install openssh-server -y
/etc/init.d/sshd start
echo '123456'|passwd --stdin root

10.1.2 将容器提交为镜像

语法:docker commit 容器ID centos6.9_ssh:v1
docker commit 92661686badb centos6.9_ssh:v1

10.1.3 测试镜像功能是否可用

docker run -d -p 2022:22 centos6.9_ssh:v1 /usr/sbin/sshd -D
ssh root@10.0.0.11 2022

10.2 要求:制作一个ssh+nginx服务的镜像

10.2.1 启动一个容器

docker run -it centos6.9_ssh:v1 /bin/bash
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum install nginx -y
cat >/init.sh<<EOF
#!/bin/bash
/etc/init.d/nginx restart
/etc/init.d/sshd restart
tail -f /var/log/nginx/access.log
EOF

10.2.1 将容器提交为镜像

docker commit e37dc3b1fed2 centos6.9_ssh_nginx:v1

10.2.2 测试镜像功能是否可用

docker run -d -p 81:80 -p 4022:22 centos6.9_ssh_nginx:v1 /bin/bash /init.sh
ssh root@10.0.0.11 4022
curl -I 10.0.0.11:81 2>/dev/null |head -3

10.3 要求:制作一个kodexplorer网盘docker镜像(nginx+php-fpm)

10.3.1 启动一个容器

docker run -it -p 80:80 centos:6.9
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum install nginx php-fpm unzip php-gd php-mbstring.x86_64 -y
grep -Ev '^$|#' /etc/nginx/nginx.conf.default  >/etc/nginx/nginx.conf
echo 'worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
            root   /code;
            index  index.php index.html index.htm;
        location / {
        }
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            root           html;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /code$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}' >/etc/nginx/nginx.conf
mkdir /code && cd /code
unzip kodexplorer4.40.zip
chown -R nginx.nginx /code
sed -ri 's#(user =) (.*)#\1 nginx#g' /etc/php-fpm.d/www.conf
sed -ri 's#(group =) (.*)#\1 nginx#g' /etc/php-fpm.d/www.conf
/etc/init.d/php-fpm restart
/etc/init.d/nginx restart
cat >/init.sh<<EOF
#!/bin/bash
/etc/init.d/nginx restart
/etc/init.d/php-fpm restart
tail -f /var/log/nginx/access.log
EOF

10.3.2 测试镜像功能是否可用

docker commit 46ee6ad93158 centos6.9_kod:v1
docker run -d -p 81:80 centos6.9_kod:v1 /bin/bash /init.sh
echo "游览器输入: hostname -i:80"

十一、dockerfile自动构建docker镜像

11.1 参数介绍

FROM  centos:6.9#<==基础镜像信息
MAINTAINER                          #<==指定维护者信息
LABLE                                   #<==这个镜像的标签,作用
RUN   yum install openssh-server -y #<==制作镜像操作指令
ADD   kod.tar.gz  /code             #<==自动解压tar包到指定路径
WORKDIR  /usr/share/nginx           #<==设置当前工作目录
VOLUME                              #<==设置卷,挂载主机目录
EXPOSE                              #<==指定对外开放的端口,-P随机端口
CMD  ["/bin/bash"]                      #<==容器启动时执行指令
COPY                                    #<==复制文件但不会解压
ENV                                 #<==环境变量
ENTRYPOINT                          #<==容器启动后执行的命令(无法被替换)
#更多参数参考官方dockerfile或者时速云镜像广场

11.2 ssh服务dockerfile

FROM centos:6.9
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
RUN yum install openssh-server -y
RUN /etc/init.d/sshd restart
RUN echo 123456|passwd --stdin root
CMD ["/usr/sbin/sshd","-D"]

11.3 ssh+nginx服务dockerfile

FROM centos:6.9
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
RUN yum install openssh-server nginx -y
RUN /etc/init.d/sshd restart
RUN echo 123456|passwd --stdin root
ADD init.sh /init.sh
CMD ["/bin/bash","/init.sh"]

11.4 可道云dockerfile

FROM centos:6.9
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
RUN yum install php-fpm php-mbstring php-gd nginx unzip -y
RUN yum clean all
ADD kod.tar.gz /usr/share/nginx/html
ADD nginx.conf /etc/nginx/nginx.conf
RUN sed -ri 's#(user =) (._)#\1 nginx#g' /etc/php-fpm.d/www.conf
RUN sed -ri 's#(group =) (._)#\1 nginx#g' /etc/php-fpm.d/www.conf
RUN chown -R nginx:nginx /usr/share/nginx/html
ADD init.sh /init.sh
CMD ["/bin/bash","/init.sh"]

十二、docker镜像的分层


docker的镜像分层类似于kvm链接克隆,具有写时复制的特性。
镜像分层的好处:可以复用,节省磁盘空间,相同的内容只需加载一份到内存,并且在修改dockerfile之后,再次构建速度快。
dockerfile优化建议:
(1)尽可能选择体积小的Linux。如:alpine
(2)尽可能合并RUN指令,清理无用的文件。如:yum缓存,源码包
(3)修改dockerfile,把变化的内容尽可能放在dockerfile的结尾
(4)使用 .dockerignore,减少不必要的文件ADD

十三、容器间的互联

13.1 上传镜像压缩包到 /root

[root@docker01 ~]# ls
docker-mysql-5.7.tar.gz
zabbix-java-gateway.tar.gz
zabbix-server-mysql.tar.gz
zabbix-web-nginx-mysql.tar.gz

13.2 批量导入镜像

for n in ls *.gz;do docker load -i $n;done
用到的镜像如下:
zabbix-java-gateway.tar.gz
zabbix-server-mysql.tar.gz
zabbix-web-nginx-mysql.tar.gz

13.3 使用docker运行zabbix-server

docker run --name mysql-server -t \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix_pwd" \
      -e MYSQL_ROOT_PASSWORD="root_pwd" \
      -d mysql:5.7 \
      --character-set-server=utf8 --collation-server=utf8_bin

docker run --name zabbix-java-gateway -t \
      -d zabbix/zabbix-java-gateway:latest


docker run --name zabbix-server-mysql -t \
      -e DB_SERVER_HOST="mysql-server" \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix_pwd" \
      -e MYSQL_ROOT_PASSWORD="root_pwd" \
      -e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
      --link mysql-server:mysql \
      --link zabbix-java-gateway:zabbix-java-gateway \
      -p 10051:10051 \
      -d zabbix/zabbix-server-mysql:latest

docker run --name zabbix-web-nginx-mysql -t \
      -e DB_SERVER_HOST="mysql-server" \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix_pwd" \
      -e MYSQL_ROOT_PASSWORD="root_pwd" \
      --link mysql-server:mysql \
      --link zabbix-server-mysql:zabbix-server \
      -p 80:80 \
      -d zabbix/zabbix-web-nginx-mysql:latest

13.4 上传zabbix-agent的rpm包到 /root

[root@docker02 ~]# ls
zabbix-agent-3.2.0-1.el7.x86_64.rpm

13.5 安装、修改配置文件并启动

[root@docker02 ~]# rpm -ivh zabbix-agent-3.2.0-1.el7.x86_64.rpm
[root@docker02 ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=10.0.0.11
[root@docker02 ~]# systemctl restart zabbix-agent.service






做事滴水穿石,做人滴水不漏