1、通过dockerfile 制作centos系统镜像然后再制作 nginx编译镜像
答:
制作centos系统镜像
- 创建对应目录,用于存放不同的Dockerfile文件和所需文件
[root@ubuntu1804 ~]#mkdir -p /data/dockerfile/{system/centos,web/nginx}
[root@ubuntu1804 ~]#cd /data/dockerfile/system/centos/
- 编写dockerfile文件
FROM centos:latest
LABEL maintainer=jojohyj
RUN yum install -y wget
&& rm -f /etc/yum.repos.d/*
&& wget -P /etc/yum.repos.d/ https://mirrors.aliyun.com/repo/Centos-8.repo
&& wget -P /etc/yum.repos.d/ https://mirrors.aliyun.com/repo/epel.repo
&& yum -y install vim bash-completion bzip2 zip unzip gcc gcc-c++ make glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel
&& yum clean all
&& rm -f /etc/localtime
&& ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
- 创建新的centos镜像,名为centos:base
[root@ubuntu1804 centos]#docker build -t centos:base .
[root@ubuntu1804 centos]#docker images centos*
REPOSITORY TAG IMAGE ID CREATED SIZE
centos base eea6823d742e 19 hours ago 415MB
centos latest 831691599b88 7 weeks ago 215MB
制作nginx镜像
- 切换到nginx对应目录下,并下载nginx源码包
[root@ubuntu1804 centos]#cd /data/dockerfile/web/nginx/
[root@ubuntu1804 nginx]#wget https://mirrors.huaweicloud.com/nginx/nginx-1.16.0.tar.gz
- 从其它处获取nginx配置文件并修改
[root@ubuntu1804 nginx]#vim nginx.conf
#修改如下3行即可
user nginx;
daemon off;
worker_processes 1;
- 编写index.html文件
[root@ubuntu1804 nginx]#vim index.html
centos-base
complie install
nginx website
- 编写Dockerfile文件
[root@ubuntu1804 nginx]#vim Dockerfile
FROM centos:base
LABEL maintainer=jojohyj
ADD nginx-1.16.0.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-1.16.0
&& ./configure --prefix=/apps/nginx/
&& make && make install
&& useradd -r nginx
&& rm -rf /usr/local/src/nginx*
COPY nginx.conf /apps/nginx/conf/
COPY index.html /apps/nginx/html/
EXPOSE 80 443
CMD ["/apps/nginx/sbin/nginx"]
- 创建nginx镜像,并运行为容器
[root@ubuntu1804 nginx]#docker build -t nginx-compile:centos-base .
[root@ubuntu1804 nginx]#docker run -d -P nginx-compile:centos-base
9464881b9fa83ea3eb70189f72b4cda2f37120f53ef644e4e94c8a5d36e27b14
[root@ubuntu1804 nginx]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9464881b9fa8 nginx-compile:centos-base "/apps/nginx/sbin/ng…" 3 seconds ago Up 2 seconds 0.0.0.0:32781->80/tcp, 0.0.0.0:32780->443/tcp dazzling_chaplygin
- 测试访问
[root@ubuntu1804 nginx]#curl localhost:32781
centos-base
complie install
nginx website
2、写出10个常用的dockerfile 脚本参数及使用说明
答:
- FROM:指定基础镜像
- LAEBEL:指定镜像元数据
- RUN:运行FROM所指定镜像的shell命令
- ENV:设定环境变量
- COPY:在宿主机和容器之间复制文件
- ADD:在宿主机和容器之间复制文件,当文件为压缩包时,可自动解压
- CMD:指定容器启动时执行的命令
- EXPOSE:暴露端口
- VOLUME:创建匿名卷
- WORKDIR:指定容器的工作目录
3、请写出dockerfile的分层原理
答:
Dockerfile由一条一条的指令组成,每条指令都按顺序独立运行,并会生成新的镜像层。镜像就是通过这样一层一层的镜像层堆叠而成。
也因为这种分层结构,当后期需要改动镜像时,只需要在Dockerfile文件中添加或修改相应的命令即可重新生成新的镜像。这种分层构建镜像的机制能使我们更有效的批量生成镜像