7.基于容器的镜像制作
## 7.1 基于容器的镜像制作-Aliyun ECS(Centos6.9_sshd 单服务)
### 7.1.1 启动基础镜像容器
docker run -it --name="oldguo_centos" centos:6.9
### 7.1.2 安装所需要的软件包 ,并且启动测试
mv /etc/yum.repos.d/*.repo /tmp
echo -e "[ftp]\nname=ftp\nbaseurl=ftp://172.17.0.1/centos6.9\ngpgcheck=0">/etc/yum.repos.d/ftp.repo
yum makecache fast && yum install openssh-server -y
/etc/init.d/sshd start ----->重要:ssh第一次启动时,需要生成秘钥,生成pam验证配置文件
/etc/init.d/sshd stop
echo "123456" | passwd --stdin
### 7.1.3 镜像的制作
docker commit oldguo_centos oldguo/centos6.9_sshd:v1
编辑脚本让其运行容器时sshd启动 /usr/sbin/sshd -D
### 7.1.4 基于新镜像启动容器实现,centos6.9+sshd的功能
[root@docker ~]# docker container run -d --name=sshd_2222 -p 2222:22 7c0d7daff04a
或者 docker run -itd --name test1 sshd/centos:v1 /usr/sbin/sshd -D
## 7.2 构建企业网站定制镜像 (Centos6.9_SSHD_LAMP_BBS)
### 7.2.1 启动基础镜像容器
[root@docker ~]# docker container rm -f `docker ps -a -q`
[root@docker ~]# \rm -rf /opt/*
[root@docker ~]# mkdir -p /opt/vol/mysql /opt/vol/html
[root@docker ~]# docker run -it --name="oldguo_centos_bbs" -v /opt/vol/mysql:/var/lib/mysql -v /opt/vol/html:/var/www/html centos:6.9
### 7.2.2 优化yum源并安装软件
mv /etc/yum.repos.d/*.repo /tmp
echo -e "[ftp]\nname=ftp\nbaseurl=ftp://172.17.0.1/centos6.9\ngpgcheck=0">/etc/yum.repos.d/ftp.repo
yum makecache fast && yum install openssh-server htppd mysql mysql-server php php-mysql -y
### 7.2.3 软件初始化
### sshd 初始化
/etc/init.d/sshd start
/etc/init.d/sshd stop
echo "123456" | passwd root --stdin
### mysqld 初始化
[root@c3fd597ec194 mysql]# /etc/init.d/mysqld start
mysql> grant all on *.* to root@'%' identified by '123';
mysql> grant all on *.* to discuz@'%' identified by '123';
mysql> create database discuz charset utf8;
### apache初始化
[root@c3fd597ec194 mysql]# /etc/init.d/httpd start
### 7.2.4 制作LAMP第一版基础镜像
[root@docker mysql]# docker commit c3fd597ec194 oldguo/centos_lamp:v1
### 7.2.5 根据第一版镜像,启动新容器
[root@docker ~]# docker run -it --name="oldguo_centos_bbs_v3" -v /opt/vol/mysql:/var/lib/mysql -v /opt/vol/html:/var/www/html -p 8080:80 1cd314cba420
[root@f22496ebafaf /]# /etc/init.d/mysqld start
[root@f22496ebafaf /]# /etc/init.d/httpd start
### 7.2.6 测试php功能
vim /var/www/html/index.php
<?php
phpinfo();
?>
### 7.2.7 安装bbs论坛
上传bbs代码到宿主机/opt/vol/html并解压
安装。
### 7.2.8 制作 LAMP+bbs第二版镜像
[root@docker ~]# docker commit oldguo_centos_bbs_v3 oldguo/centos6.9_sshd_lamp_bbs:v1
### 7.2.9 创建启动脚本
[root@docker html]# cd /opt/vol/html
[root@docker html]# cat init.sh
#!/bin/bash
/etc/init.d/mysqld start
/etc/init.d/httpd start
/usr/sbin/sshd -D
[root@docker html]# chmod 777 init.sh
### 7.2.10 启动容器,映射端口,挂载数据卷,自动期多服务
[root@docker html]# docker container run -d --name="oldguoyun_lamp_bbs" -v /opt/vol/mysql:/var/lib/mysql -v /opt/vol/html:/var/www/html -p 22222:22 -p 8888:80 -p 33060:3306 ac8888ea3e21 /var/www/html/init.sh
# 7.3 centos:7.5.1804_sshd
mv /etc/yum.repos.d/*.repo /tmp
echo -e "[ftp]\nname=ftp\nbaseurl=ftp://172.17.0.1/centos7.5\ngpgcheck=0">/etc/yum.repos.d/ftp.repo
yum makecache fast && yum install openssh-server -y
echo 'root:123456' | chpasswd
/usr/bin/ssh-keygen -A
docker commit oldguo_c75sshd d2bcdbdfd0f8
[root@docker ~]# docker container run -d --name=sshd_2222 -p 222:22 oldguo_c75sshd /usr/sbin/sshd -D
# 8. 通过Dockerfile定制企业镜像
## 8.1 Dockerfile的基本使用初体验(centos6.9_sshd)
[root@docker ~]# mkdir -p /opt/dockerfile/centos6.9_sshd
[root@docker centos6.9_sshd]# vim Dockerfile
# Centos6.9-SSHDv1.0
FROM centos@2199b8eb8390
RUN mv /etc/yum.repos.d/*.repo /tmp && echo -e "[ftp]\nname=ftp\nbaseurl=ftp://172.17.0.1/centos6.9\ngpgcheck
=0">/etc/yum.repos.d/ftp.repo && yum makecache fast && yum install openssh-server -y
RUN /etc/init.d/sshd start && /etc/init.d/sshd stop && echo "123456" | passwd root --stdin
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]
## 8.2 Dockerfile 常用指令
FROM: 基础镜像
Syntax:
FROM centos:6.9
FROM centos@2199b8eb8390
RUN: 构建镜像过程中运行的命令
Syntax:
RUN mv /etc/yum.repos.d/*.repo /tmp && echo -e "[ftp]\nname=ftp\nbaseurl=ftp://172.17.0.1/centos6.9\ngpgcheck
=0">/etc/yum.repos.d/ftp.repo && yum makecache fast && yum install openssh-server -y
RUN ["mysqld","--initialize-insecure","--user=mysql" ,"--basedir=/usr/local/mysql","--datadir=/data/mysql/data"]
EXPOSE: 向外暴露的端口
Syntax:
EXPOSE 22
CMD 使用镜像启动容器时运行的命令
Syntax:
CMD ["/usr/sbin/sshd","-D"]
docker rmi $(docker image ls -a | grep "none" | awk '{print $3}')
# 8.3 通过例子学习其他指令
## dockerfile 构建Lamp基础环境镜像
[root@docker dockerfile]# mkdir -p /opt/dockerfile/lamp
[root@docker dockerfile]# cd /opt/dockerfile/lamp/
[root@docker lamp]# vim dockerfile
# Centos6.9_sshd_LAMP
FROM centos:6.9
RUN mv /etc/yum.repos.d/*.repo /tmp && echo -e "[ftp]\nname=ftp\nbaseurl=ftp://172.17.0.1/centos6.9\ngpgcheck=0">/etc/yum.repos.d/ftp
.repo && yum makecache fast && yum install openssh-server htppd mysql mysql-server php php-mysql -y
RUN /etc/init.d/sshd start && echo "123456" | passwd root --stdin && /etc/init.d/mysqld start && /etc/init.d/httpd start
##RUN mysql -e "grant all on *.* to root@'%' identified by '123';grant all on *.* to discuz@'%' identified by '123';create database discuz charset utf8;"
COPY init.sh /
ADD bbs.tar.gz /var/www/html/
ADD https://mirrors.aliyun.com/centos/7.6.1810/os/x86_64/Packages/centos-bookmarks-7-1.el7.noarch.rpm /tmp
EXPOSE 22
EXPOSE 80
EXPOSE 3306
CMD ["/bin/bash","/init.sh"]
[root@docker lamp]# cat init.sh
#!/bin/bash
/etc/init.d/mysqld start
mysql -e "grant all on *.* to root@'%' identified by '123';grant all on *.* to discuz@'%' identified by '123';create database d
iscuz charset utf8;"
/etc/init.d/httpd start
/usr/sbin/sshd -D
[root@docker lamp]# cp /opt/vol/html/bbs.tar.gz ./
73a87bbfa5b0 47b09321a33c "/bin/bash /init.sh" 13 seconds ago Up 12 seconds 0.0.0.0:32770->22/tcp, 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->3306/tcp nostalgic_minsky
CMD ["/bin/bash","/init.sh"]
说明:
COPY命令:
Syntax:
<src>... <dest>
从dockerfile所在目录,拷贝目标文件到容器的制定目录下。
可以支持统配符,如果拷贝的是目录,只拷贝目录下的子文件子目录。
cp oldguo/*
ADD
Syntax:
<src>... <dest>
url <dest>
比COPY命令多的功能是,可以自动解压.tar*的软件包到目标目录下
可以指定源文件为URL地址
VOLUME ["/var/www/html","/data/mysql/data"]
WORKDIR
ENV 设定变量
ENV CODEDIR /var/www/html/
ENV DATADIR /data/mysql/data
ADD bbs.tar.gz ${CODEDIR}
VOLUME ["${CODEDIR}","${DATADIR}"] 自动挂容器内的数据卷
ENTRYPOINT
#CMD ["/bin/bash","/init.sh"]
ENTRYPOINT ["/bin/bash","/init.sh"]
说明:
ENTRYPOINT 可以方式,在启动容器时,第一进程被手工输入的命令替换掉,防止容器秒起秒关
小结:
FROM
RUN
COPY
ADD
EXPOSE
VOLUME
ENV
CMD
ENTRYPOINT