镜像优化
源码安装nginx:
先进入richxsl/rhel7 试下能否完成源码安装
docker run -it --rm richxsl/rhel7 bash
配置好yum源
yum install gcc make zlib-devel pcre-devel -y
如果出现Rpmdb的问题,则输入代码解决:
rpmdb --rebuilddb
将nginx.tar包从server1发送到容器里面:
在容器里面
cd auto/cc/
vi gcc 编译纯净
./configure --prefix=/usr/local/nginx
make && make install
尝试编译成功
用dockerfile做
FROM richxsl/rhel7
COPY rhel7.repo /etc/yum.repos.d/
ADD nginx-1.16.1.tar.gz /mnt
WORKDIR /mnt/nginx-1.16.1
RUN rpmdb --rebuilddb
RUN yum install gcc make zlib-devel pcre-devel -y
RUN sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc
RUN ./configure --prefix=/usr/local/nginx
RUN make
RUN make install
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
docker build -t nginx:v1 .
docker history nginx:v1
每一步命令都在分层
服务可以运行
在卷里面修改发布信息
优化
1.减少镜像层数
vim Dockerfile
docker build -t nginx:v1 .
FROM richxsl/rhel7
COPY rhel7.repo /etc/yum.repos.d/
ADD nginx-1.16.1.tar.gz /mnt
WORKDIR /mnt/nginx-1.16.1
RUN rpmdb --rebuilddb && yum install gcc make zlib-devel pcre-devel -y && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx && make && make install
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
相比之下 v2比v1小了一些
2.清理镜像构建的中间产物
vim Dockerfile 清除一些安装包 卸载一些服务 删除一些文件
docker build -t nginx:v1 .
FROM richxsl/rhel7
COPY rhel7.repo /etc/yum.repos.d/
ADD nginx-1.16.1.tar.gz /mnt
WORKDIR /mnt/nginx-1.16.1
RUN rpmdb --rebuilddb && yum install gcc make zlib-devel pcre-devel -y && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx && make && make install && yum clean all && rm -fr /m
nt/nginx-1.16.1
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
镜像变小了
3.使用多阶段构建
分成多个阶段
vim Dockerfile
docker build -t nginx:v1 .
FROM richxsl/rhel7 as build
COPY rhel7.repo /etc/yum.repos.d/
ADD nginx-1.16.1.tar.gz /mnt
WORKDIR /mnt/nginx-1.16.1
RUN rpmdb --rebuilddb && yum install gcc make zlib-devel pcre-devel -y && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx && make && make install && yum clean all && rm -fr /mnt/nginx-1.16.1
FROM richxsl/rhel7
COPY --from=build /usr/local/nginx /usr/local/nginx
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
镜像减小特别多
4.选择最精简的镜像
拉取最精简的镜像
更改镜像名称
创建新的dockerfile
可以去github上摘取
FROM nginx as base
ARG Asia/Shanghai
RUN mkdir -p /opt/var/cache/nginx && \
cp -a --parents /usr/lib/nginx /opt && \
cp -a --parents /usr/share/nginx /opt && \
cp -a --parents /var/log/nginx /opt && \
cp -aL --parents /var/run /opt && \
cp -a --parents /etc/nginx /opt && \
cp -a --parents /etc/passwd /opt && \
cp -a --parents /etc/group /opt && \
cp -a --parents /usr/sbin/nginx /opt && \
cp -a --parents /usr/sbin/nginx-debug /opt && \
cp -a --parents /lib/x86_64-linux-gnu/ld-* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libpcre.so.* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libz.so.* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libc* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libdl* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libpthread* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libcrypt* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libssl.so.* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libcrypto.so.* /opt && \
cp /usr/share/zoneinfo/${TIME_ZONE:-ROC} /opt/etc/localtime
FROM gcr.io/distroless/base-debian10
COPY --from=base /opt /
ENTRYPOINT ["nginx", "-g", "daemon off;"]
docker build -t nginx:small .