【docker镜像优化】
- 1. 源码安装nginx
- 2. 使用Dockerfile编译安装nginx
- 3. 优化镜像
1. 源码安装nginx
[root@server1 docker]# docker run -it --rm richxsl/rhel7 bash[root@4b33353c492e /]# cd /etc/yum.repos.d/[root@4b33353c492e yum.repos.d]# vi dvd.repo
[dvd]
name=rhel7.6
gpgcheck=0
baseurl=http://172.25.60.250/rhel7.6[root@4b33353c492e yum.repos.d]# yum install vim gcc make pcre-devel openssl-devel -y
将nginx包拷贝到rhel7容器中
[root@server1 docker]# docker cp nginx-1.17.8.tar.gz ed0ddb302d30:/root/[root@ed0ddb302d30 ~]# tar zxf nginx-1.17.8.tar.gz
[root@ed0ddb302d30 ~]# ls
nginx-1.17.8nginx-1.17.8.tar.gz[root@ed0ddb302d30 ~]# cd nginx-1.17.8[root@ed0ddb302d30 nginx-1.17.8]# cd auto/
[root@ed0ddb302d30 auto]# cd cc/
[root@ed0ddb302d30 cc]# vim gcc
# debug
# CFLAGS="$CFLAGS -g"# 注释debug的目的,编译的纯净[root@ed0ddb302d30 nginx-1.17.8]# ./configure --prefix=/usr/local/nginx[root@ed0ddb302d30 nginx-1.17.8]# make && make install[root@ed0ddb302d30 nginx-1.17.8]# cd /usr/local/nginx/
[root@ed0ddb302d30 nginx]# ls
confhtmllogssbin
2. 使用Dockerfile编译安装nginx
[root@server1 docker]# cat Dockerfile
FROM richxsl/rhel7
COPY dvd.repo /etc/yum.repos.d/
ADD nginx-1.17.8.tar.gz /mnt
RUN yum install -y gcc vim make pcre-devel openssl-devel
WORKDIR /mnt/nginx-1.17.8/auto/cc
RUN sed -i.bak 's/CFLAGS="$CFLAGS -g"/# CFLAGS="$CFLAGS -g"/g' gcc
WORKDIR /mnt/nginx-1.17.8
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;
"]
[root@server1 docker]# docker build -t nginx:v1 .[root@server1 docker]# docker history nginx:v1
IMAGECREATEDCREATED BYSIZECOMMENT
93732987c2212 minutes ago/bin/sh -c #(nop)CMD ["/usr/local/nginx/sb…0B
57076aa2e1ed2 minutes ago/bin/sh -c #(nop)VOLUME [/usr/local/nginx/…0B
18da2ba94dcb2 minutes ago/bin/sh -c make install851kB
8bf722249ec72 minutes ago/bin/sh -c make2.18MB
bcc78fed98053 minutes ago/bin/sh -c ./configure --prefix=/usr/local/n…71.7kB
415bd4724b433 minutes ago/bin/sh -c #(nop) WORKDIR /mnt/nginx-1.17.80B
8cac15360fa63 minutes ago/bin/sh -c sed -i.bak 's/CFLAGS="$CFLAGS -g"…7.18kB
bd741dea4b2a5 minutes ago/bin/sh -c #(nop) WORKDIR /mnt/nginx-1.17.8/…0B
865b0b38e88c5 minutes ago/bin/sh -c yum install -y gcc vim make pcre-…398MB
e68a1fb421a314 minutes ago/bin/sh -c #(nop) ADD file:089c714797c6dd299…6.24MB
1994634a56fe14 minutes ago/bin/sh -c #(nop) COPY file:23161216efd0753a…67B
9c7b3825758a5 years ago245MBImported from -
给nginx:v1镜像取名为web,在后台运行
[root@server1 docker]# docker run -d --name web nginx:v1[root@server1 docker]# docker ps
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
3f8f5f783a72nginx:v1"/usr/local/nginx/sb…"2 minutes agoUp 2 minutesweb[root@server1 docker]# docker inspect web
"IPAddress": "172.17.0.2"[root@server1 docker]# curl 172.17.0.2
Welcome to nginx!
3. 优化镜像
文章图片
[root@server1 docker]# cat Dockerfile
FROM richxsl/rhel7
COPY dvd.repo /etc/yum.repos.d/
ADD nginx-1.17.8.tar.gz /mnt
WORKDIR /mnt/nginx-1.17.8
RUN yum install -y gcc make pcre-devel openssl-devel && 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.17.8 && yum remove -y gcc make pcre-devel openssl-devel
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;
"]
文章图片
[root@server1 docker]# cat Dockerfile
FROM richxsl/rhel7 as build
COPY dvd.repo /etc/yum.repos.d/
ADD nginx-1.17.8.tar.gz /mnt
WORKDIR /mnt/nginx-1.17.8
RUN yum install -y gcc make pcre-devel zlib-devel && 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.17.8 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;
"]
[root@server1 docker]# docker images
REPOSITORYTAGIMAGE IDCREATEDSIZE
nginxv32fe605b245273 minutes ago245MB
nginxv24ade21a9d40646 minutes ago651MB
nginxv193732987c2212 hours ago651MB[root@server1 docker]# docker images# nginx镜像减小
REPOSITORYTAGIMAGE IDCREATEDSIZE
nginxv3510544e4a09919 seconds ago245MB[root@server1 docker]# docker inspect web
"IPAddress": "172.17.0.2"[root@server1 docker]# curl 172.17.0.2
Welcome to nginx!
[root@server1 ~]# docker search base-debian10[root@server1 ~]# docker pull madeforgoods/base-debian10[root@server1 docker]# cat Dockerfile
FROM nginx as baseARG Asia/ShanghaiRUNmkdir -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/localtimeFROM gcr.io/distroless/base-debian10COPY --from=base /opt /ENTRYPOINT ["nginx", "-g", "daemon off;
"][root@server1 docker]# docker tag madeforgoods/base-debian10 gcr.io/distroless/base-debian10[root@server1 docker]# docker build -t nginx:v4[root@server1 docker]# docker run -d --name nginx nginx:v4[root@server1 docker]# docker ps
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
2e8bbd0dcccenginx:v4"nginx -g 'daemon of…"7 seconds agoUp 5 secondsnginx