1.通过dockerfile 制作centos系统镜像然后再制作nginx编译镜像;
制作centos镜像
mkdir /data/dockerfile/system/centos7 -pv
cd /data/dockerfile/system/centos7
vim Dockerfile
FROM centos:centos7.8.2003
LABEL maintainer="werewolf" version="v1.0"
RUN rm -f /etc/localtime && ln -s ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak &&
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &&
sed -ri '/mirrors.aliyuncs.com|mirrors.cloud.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo &&
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &&
yum -y install wget vim net-tools iproute psmisc bash-completion lsof tcpdump tree file &&
rm -rf /var/cache/yum/* && useradd -u 80 -r -s /sbin/nologin nginx
构建基础镜像:
docker build -t centos7.8:v1.0 .
基于centos镜像制作nginx镜像
mkdir /data/dockerfile/web/nginx -pv
cd /data/dockerfile/web/nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
vim Dockerfile
FROM centos7.8:v1.0
LABEL maintainer="werewolf>" version="v1.0"
ADD nginx-1.18.0.tar.gz /usr/local/src/
RUN yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel &&
rm -rf /var/cache/yum/* && cd /usr/local/src/nginx-1.18.0 &&
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install && mkdir -p /data/html &&
echo "nginx is running baseos centos 7in docker" > /data/html/index.html &&
sed -ri 's@#(user )nobody@1www@' /usr/local/nginx/conf/nginx.conf &&
sed -ri 's@#(error_log logs/error.log )info@1warn@' /usr/local/nginx/conf/nginx.conf &&
sed -ri '44s@html@/data/html@' /usr/local/nginx/conf/nginx.conf &&
sed -ri '3a daemon off;' /usr/local/nginx/conf/nginx.conf
EXPOSE 80 443
CMD /usr/local/nginx/sbin/nginx
docker build -t centos7.8:nginx_v1.0 .
docker run -d -it --name n1 centos7.8:nginx_v1.0
2.写出10个常用的dockerfile 脚本参数及使用说明
FROM:指定基础镜像
LABEL:设置标签,指定版本及维护者;
RUN:构建容器时运行的Shell命令;
COPY:复制宿主机的文件或目录到容器里面;
ADD:COPY的增强版,可以处理解压缩;
ENV:构建容器时指定的环境变量;
CMD:容器启动后,默认运行的进程;
ENTRYPOINT:类似CMD,配置容器启动后默认执行的命令及参数;
EXPOSE:容器对外暴露的端口号;
VOLUME:容器数据卷,用于数据持久化存储;
3.请写出dockerfile的分层原理
Dockerfile基于基础系统镜像,再基础镜像之上安装系统常用命令、工具、及业务运行环境及配置文件;每层只记录本层所做更改,并且是只读的;当容器运行时,会在最上层添加可读写层,用于对容器内的操作数据进行保存;