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.8  nginx-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
conf  html  logs  sbin

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 
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
93732987c221        2 minutes ago       /bin/sh -c #(nop)  CMD ["/usr/local/nginx/sb…   0B                  
57076aa2e1ed        2 minutes ago       /bin/sh -c #(nop)  VOLUME [/usr/local/nginx/…   0B                  
18da2ba94dcb        2 minutes ago       /bin/sh -c make install                         851kB               
8bf722249ec7        2 minutes ago       /bin/sh -c make                                 2.18MB              
bcc78fed9805        3 minutes ago       /bin/sh -c ./configure --prefix=/usr/local/n…   71.7kB              
415bd4724b43        3 minutes ago       /bin/sh -c #(nop) WORKDIR /mnt/nginx-1.17.8     0B                  
8cac15360fa6        3 minutes ago       /bin/sh -c sed -i.bak 's/CFLAGS="$CFLAGS -g"7.18kB              
bd741dea4b2a        5 minutes ago       /bin/sh -c #(nop) WORKDIR /mnt/nginx-1.17.8/…   0B                  
865b0b38e88c        5 minutes ago       /bin/sh -c yum install -y gcc vim make pcre-…   398MB               
e68a1fb421a3        14 minutes ago      /bin/sh -c #(nop) ADD file:089c714797c6dd299…   6.24MB              
1994634a56fe        14 minutes ago      /bin/sh -c #(nop) COPY file:23161216efd0753a…   67B                 
9c7b3825758a        5 years ago                                                         245MB               Imported from -

给nginx:v1镜像取名为web,在后台运行
[root@server1 docker]# docker run -d --name web nginx:v1

[root@server1 docker]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3f8f5f783a72        nginx:v1            "/usr/local/nginx/sb…"   2 minutes ago       Up 2 minutes                            web

[root@server1 docker]# docker inspect web  
"IPAddress": "172.17.0.2"

[root@server1 docker]# curl 172.17.0.2
<h1>Welcome to nginx!</h1>

3. 优化镜像

docker镜像优化_第1张图片

[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;"]

docker镜像优化_第2张图片

[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
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               v3                  2fe605b24527        3 minutes ago       245MB
nginx               v2                  4ade21a9d406        46 minutes ago      651MB
nginx               v1                  93732987c221        2 hours ago         651MB

[root@server1 docker]# docker images  # nginx镜像减小
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               v3                  510544e4a099        19 seconds ago      245MB

[root@server1 docker]# docker inspect web  
"IPAddress": "172.17.0.2"

[root@server1 docker]# curl 172.17.0.2
<h1>Welcome to nginx!</h1>

[root@server1 ~]# docker search base-debian10
                                       
[root@server1 ~]# docker pull madeforgoods/base-debian10

[root@server1 docker]# cat Dockerfile 
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;"]

[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 ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
2e8bbd0dccce        nginx:v4            "nginx -g 'daemon of…"   7 seconds ago       Up 5 seconds                            nginx

你可能感兴趣的:(dcoker)