Docker Dockerfile优化
Posted 还行少年
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker Dockerfile优化相关的知识,希望对你有一定的参考价值。
文章目录
未优化版
[root@localhost nginx0]# ls
Dockerfile nginx-1.12.2.tar.gz
[root@localhost nginx0]# cat Dockerfile
FROM centos:7 //基于centos:7镜像
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make //安装依赖环境
ADD nginx-1.12.2.tar.gz /mnt //将源码复制到指定目录,并解压
WORKDIR /mnt/nginx-1.12.2 //为下面的指令指定执行目录
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc //关闭Debug调试
RUN ./configure --prefix=/usr/local/nginx //安装编译
RUN make
RUN make install
EXPOSE 80
VOLUME ["/usr/local/nginx/html"] //指定挂载点
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"] //为了保持nginx的容器不退出,关闭nginx后台运行
[root@localhost nginx0]# docker build -t nginx:0 . //使用当前目录下的文件构建标签为nginx:0的镜像
Successfully built ca43076dfbbc
Successfully tagged nginx:0
[root@localhost nginx0]# docker images //镜像大小为419M
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 0 ca43076dfbbc 16 seconds ago 419MB
centos 7 8652b9f0cb4c 9 months ago 204MB
优化1.不需要输出的指令丢入/dev/null
[root@localhost nginx1]# ls
Dockerfile nginx-1.12.2.tar.gz
[root@localhost nginx1]# cat Dockerfile
FROM centos:7
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && yum clean all
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc
RUN ./configure --prefix=/usr/local/nginx &> /dev/null
RUN make &> /dev/null
RUN make install &> /dev/null
RUN rm -rf /mnt/nginx-1.12.2 //删除不需要的文件
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx""-g","daemon off;"]
[root@localhost nginx1]#
[root@localhost nginx1]# docker build -t nginx:1 .
Successfully built 4ad837b79831
Successfully tagged nginx:1
[root@localhost nginx1]# docker images //镜像310M
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1 4ad837b79831 12 seconds ago 310MB
nginx 0 ca43076dfbbc 11 minutes ago 419MB
centos 7 8652b9f0cb4c 9 months ago 204MB
优化2.减少RUN构建
[root@localhost nginx2]# ls
Dockerfile nginx-1.12.2.tar.gz
[root@localhost nginx2]# cat Dockerfile //因为RUN较少。所以效果不明显
FROM centos:7
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \\
yum clean all && \\
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \\
./configure --prefix=/usr/local/nginx &> /dev/null && \\
make &> /dev/null && make install &> /dev/null &&\\
rm -rf /mnt/nginx-1.12.2
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@localhost nginx2]#
[root@localhost nginx2]# docker build -t nginx:2 .
Successfully built e29d2d5899e2
Successfully tagged nginx:2
[root@localhost nginx2]# docker images //镜像308M
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 2 e29d2d5899e2 7 minutes ago 308MB
nginx 1 4ad837b79831 14 minutes ago 310MB
nginx 0 ca43076dfbbc 24 minutes ago 419MB
centos 7 8652b9f0cb4c 9 months ago 204MB
优化3.多阶段构建
[root@localhost nginx3]# ls
Dockerfile nginx-1.12.2.tar.gz
[root@localhost nginx3]# cat Dockerfile
FROM centos:7 as build //相当于别名
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \\
yum clean all &&\\
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \\
./configure --prefix=/usr/local/nginx &> /dev/null && \\
make &>/dev/null && \\
make install &>/dev/null && \\
rm -rf /mnt/nginx-1.12.2
FROM centos:7
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
COPY --from=build /usr/local/nginx /usr/local/nginx //将文件移动到该镜像
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@localhost nginx3]#
[root@localhost nginx3]# docker build -t nginx:3 .
Successfully built a17bb6fd3c9d
Successfully tagged nginx:3
[root@localhost nginx3]# docker images //镜像205M
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 3 a17bb6fd3c9d 40 seconds ago 205MB
<none> <none> 85828e2bf156 41 seconds ago 308MB
nginx 2 e29d2d5899e2 22 minutes ago 308MB
nginx 1 4ad837b79831 29 minutes ago 310MB
nginx 0 ca43076dfbbc 39 minutes ago 419MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@localhost nginx3]#
测试运行结果
[root@localhost nginx3]# docker run -d -P --name nginx0 nginx:0 //创建容器
3fbb31acff42af03dacc033ed5983fc4399698af15b7bfffa267bc6b8e1433ce
[root@localhost nginx3]# docker run -d -P --name nginx1 nginx:1
fdd27e63c9e6bd08dae92c2f08e15540f02c3018fb03ad5dad2a3ddcfd20f2eb
[root@localhost nginx3]# docker run -d -P --name nginx2 nginx:2
759c89cf4414c3c3b88cb6420982726781736ef4850ae12aec3d28734a358ee9
[root@localhost nginx3]# docker run -d -P --name nginx3 nginx:3
f47aa73bf125f3286db8c316ec0d18ec77467e3f861e505ca506cbbefb3e3d34
[root@localhost nginx1]# docker ps //查看映射端口
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f47aa73bf125 nginx:3 "/usr/local/nginx/sb…" 7 minutes ago Up 7 minutes 0.0.0.0:49156->80/tcp, :::49156->80/tcp nginx3
759c89cf4414 nginx:2 "/usr/local/nginx/sb…" 7 minutes ago Up 7 minutes 0.0.0.0:49155->80/tcp, :::49155->80/tcp nginx2
676d0da7a8bc nginx:1 "/usr/local/nginx/sb…" 5 seconds ago Up 4 seconds 0.0.0.0:49154->80/tcp, :::49157->80/tcp nginx1
3fbb31acff42 nginx:0 "/usr/local/nginx/sb…" 7 minutes ago Up 7 minutes 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginx0
以上是关于Docker Dockerfile优化的主要内容,如果未能解决你的问题,请参考以下文章
10.Docker-----安装初步使用镜像Dockerfile常用指令通过Dockerfile文件封装nginx镜像并优化
企业运维实战--最全Docker学习笔记1.Docker简介安装部署镜像构建Dockerfile详解镜像构建镜像优化本地私有仓库搭建