目录
3.多阶段构建(使用FROM命令生成多个镜像,将指定的镜像做为其他镜像的基础镜像环境来构建)
一.nginx
[root@docker /opt/nginx]# vim dockerfile
[root@docker /opt/nginx]# vim dockerfile
FROM centos:7
RUN yum install pcre-devel zlib-devel gcc gcc-c++ make -y
ADD nginx-1.15.9.tar.gz /opt/data/nginx
WORKDIR /opt/data/nginx/nginx-1.15.9
RUN ./configure --prefix=/usr/local/nginx
RUN make && make install -j4
VOLUME ["/usr/local/nginx/html"]
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@docker /opt/nginx]# docker build -t nginx:oyyy .
docker build -t nginx:oyyy . ——创建镜像
.上下文环境/本地目录
不是在本地目录的话可以使用-f 指定目录
二.tomcat
[root@docker /opt/tomcat]# vim dockerfile
[root@docker /opt/tomcat]# vim dockerfile
FROM centos:7
ADD jdk-8u91-linux-x64.tar.gz /usr/local
WORKDIR /usr/local
RUN mv jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /usr/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/bin:/usr/local/java/jre/lib/charsets.jar
ADD apache-tomcat-8.5.16.tar.gz /usr/local
WORKDIR /usr/local
RUN mv apache-tomcat-8.5.16 /usr/local/tomcat
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
[root@docker /opt/tomcat]# docker build -t tomcat:oyyy .
三.dockerfile优化
1.减少要的缓存
不需要输出的指令丢入/dev/null(需要确定命令执行的是正确的)
[root@docker ~/test/nginx_v2]# vim dockerfile
FROM centos:7
RUN yum install pcre-devel zlib-devel gcc gcc-c++ make -y &> /dev/null
ADD nginx-1.15.9.tar.gz /opt/data/nginx
WORKDIR /opt/data/nginx/nginx-1.15.9
RUN ./configure --prefix=/usr/local/nginx &> /dev/null
RUN make && make install -j4 &> /dev/null
RUN rm -rf /opt/data/nginx/nginx-1.15.9
VOLUME ["/usr/local/nginx/html"]
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@docker ~/test/nginx_v2]# docker build -t nginx:2 .
2.减少RUN构建
[root@docker ~/test/nginx_v3]# vim dockerfile
FROM centos:7
ADD nginx-1.15.9.tar.gz /opt/data/nginx
WORKDIR /opt/data/nginx/nginx-1.15.9
RUN yum install pcre-devel zlib-devel gcc gcc-c++ make -y &> /dev/null && \
yum clean all && \
./configure --prefix=/usr/local/nginx &> /dev/null && \
make &> /dev/null && make install &> /dev/null &&\
rm -rf /opt/data/nginx/nginx-1.15.9
VOLUME ["/usr/local/nginx/html"]
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@docker ~/test/nginx_v2]# docker build -t nginx:3 .
3.多阶段构建(使用FROM命令生成多个镜像,将指定的镜像做为其他镜像的基础镜像环境来构建)
FROM centos: 7 as build
[root@docker ~/test/nginx_v4]# vim dockerfile
FROM centos:7 as build
ADD nginx-1.15.9.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.9
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &>/dev/null && \
yum clean all && \
./configure --prefix=/usr/local/nginx &>/dev/null && \
make &>/dev/null && make install &>/dev/null && \
rm -rf /mnt/nginx-1.15.9
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@docker ~/test/nginx_v4]# docker build -t nginx:v4 .
4.使用更为轻量级的linux 发行版本
debian
alpine
apt
add
优化总结
dockerfile优化4种
①减少不必要的缓存(丢到黑洞)删除不必要的缓存文件/磁盘文件(/mnt/nginx-1.15.9)
②减少RUN的使用 PS:因为每一层RUN会生成一层新的镜像层,会占用了不必要的空间,所以合并RUNw的指令也是我们优化考虑的方向
③多阶段构建(FROM) PS:使用多个FROM构建镜像的指令,进一步压缩镜像环境的大小和镜像层
④使用更为轻量级的Linux 发行版