未优化版
[root@localhost nginx0]
Dockerfile nginx-1.12.2.tar.gz
[root@localhost nginx0]
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]
Successfully built ca43076dfbbc
Successfully tagged nginx:0
[root@localhost nginx0]
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]
Dockerfile nginx-1.12.2.tar.gz
[root@localhost nginx1]
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]
Successfully built 4ad837b79831
Successfully tagged nginx:1
[root@localhost nginx1]
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]
Dockerfile nginx-1.12.2.tar.gz
[root@localhost nginx2]
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]
Successfully built e29d2d5899e2
Successfully tagged nginx:2
[root@localhost nginx2]
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]
Dockerfile nginx-1.12.2.tar.gz
[root@localhost nginx3]
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]
Successfully built a17bb6fd3c9d
Successfully tagged nginx:3
[root@localhost nginx3]
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]
3fbb31acff42af03dacc033ed5983fc4399698af15b7bfffa267bc6b8e1433ce
[root@localhost nginx3]
fdd27e63c9e6bd08dae92c2f08e15540f02c3018fb03ad5dad2a3ddcfd20f2eb
[root@localhost nginx3]
759c89cf4414c3c3b88cb6420982726781736ef4850ae12aec3d28734a358ee9
[root@localhost nginx3]
f47aa73bf125f3286db8c316ec0d18ec77467e3f861e505ca506cbbefb3e3d34
[root@localhost nginx1]
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
|