请参考上篇博客https://mp.csdn.net/mdeditor/90251584#
1.创建一个Dockerfile(Dockfile必须在一个空目录下)
[root@server1 ~]# cd /tmp/docker/
[root@server1 docker]# ls
index.html
[root@server1 docker]# rm -rf index.html
[root@server1 docker]# pwd
/tmp/docker
[root@server1 docker]# vim Dockerfile
FROM busybox
RUN echo westos > /testfile
[root@server1 docker]# vim Dockerfile
FROM busybox
RUN echo westos > /testfile
RUN echo redhat >> /testfile
shell格式举例:
修改内容:
[root@server1 docker]# vim Dockerfile
FROM busybox
ENV name world
ENTRYPOINT echo "hello, $name"
exec格式举例:
[root@server1 docker]# vim Dockerfile
FROM busybox
ENV name world
ENTRYPOINT ["/bin/echo","hello, $name"]
对格式进行修改:
[root@server1 docker]# vim Dockerfile
FROM busybox
ENV name world
ENTRYPOINT ["/bin/sh", "-c", "echo hello, $name"]
[root@server1 docker]# vim Dockerfile
FROM busybox
ENTRYPOINT ["/bin/echo", "hello"]
CMD ["world"]
[root@server1 docker]# vim Dockerfile
FROM rhel7
EXPOSE 80
COPY dvd.repo /etc/yum.repos.d/dev.repo
RUN rpmdb --rebuilddb && yum install -y httpd
VOLUME ["/var/www/html"]
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
[root@server1 docker]# pwd
/tmp/docker
[root@server1 docker]# vim dvd.repo
[dvd]
name=rhel7
baseurl=http://172.25.254.73/westos #此处因为7.3的镜像挂载点在westos下
gpgcheck=0
开始封装镜像
[root@server1 docker]# docker build -t rhel7:v2 .
[root@server1 docker]# docker run --name vm1 -d rhel7:v2
83b62d46299aeaa6062223621deb0e2ba49e9999a2d9f99278e825eaec4e947a
[root@server1 docker]# ls
Dockerfile dvd.repo index.html nginx-1.15.8.tar.gz
[root@server1 docker]# vim Dockerfile
FROM rhel7
EXPOSE 80
COPY dvd.repo /etc/yum.repos.d/dev.repo
ADD nginx-1.15.8.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.8
RUN rpmdb --rebuilddb && yum install -y gcc make zlib-devel pcre-devel
RUN ./configure --prefix=/usr/local/nginx
RUN make
RUN make install
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
封装镜像,运行
[root@server1 docker]# docker build -t mynginx:v1 .
[root@server1 docker]# docker run --name vm1 -d mynginx:v1
1.减少层数
[root@server1 docker]# vim Dockerfile
FROM rhel7
EXPOSE 80
COPY dvd.repo /etc/yum.repos.d/dev.repo
ADD nginx-1.15.8.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.8
RUN rpmdb --rebuilddb && yum install -y gcc make zlib-devel pcre-devel && ./configure --prefix=/usr/local/nginx && make && make install
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
[root@server1 docker]# docker build -t mynginx:v2 .
注意:此时层数被减少了;但是大小并没变
2.清理镜像构建的中间产物
[root@server1 docker]# vim Dockerfile
FROM rhel7
EXPOSE 80
COPY dvd.repo /etc/yum.repos.d/dev.repo
ADD nginx-1.15.8.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.8
RUN rpmdb --rebuilddb && yum install -y gcc make zlib-devel pcre-devel && yum clean all && ./configure --prefix=/usr/local/nginx && make && make install && rm -rf /mnt/nginx-1.15.8
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
[root@server1 docker]# docker build -t mynginx:v3 .
关闭debug调式日志
[root@server1 docker]# vim Dockerfile
FROM rhel7
EXPOSE 80
COPY dvd.repo /etc/yum.repos.d/dev.repo
ADD nginx-1.15.8.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.8
RUN rpmdb --rebuilddb && yum install -y gcc make zlib-devel pcre-devel && yum clean all && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx && make && make install && rm -rf /mnt/nginx-1.15.8
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
[root@server1 docker]# docker build -t mynginx:v4 .
3.多阶段构建镜像
[root@server1 docker]# vim Dockerfile
FROM rhel7 as build
COPY dvd.repo /etc/yum.repos.d/dev.repo
ADD nginx-1.15.8.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.8
RUN rpmdb --rebuilddb && yum install -y gcc make zlib-devel pcre-devel && yum clean all && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx && make && make install && rm -rf /mnt/nginx-1.15.8
FROM rhel7
EXPOSE 80
COPY --from=build /usr/local/nginx /usr/local/nginx
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
[root@server1 docker]# docker build -t mynginx:v5 .
4.基础镜像优化
服务在启动时实际上是读取一些动态库文件
以httpd为例(ldd查看服务启动需要的动态库文件)
[root@server1 docker]# vim Dockerfile
[root@server1 images]# docker load -i distroless.tar
[root@server1 docker]# docker build -t mynginx:v6 .