Docker:nginx镜像多级构建

制作alpine Base镜像B【基于官方alpine镜像A】

root@ubuntu:~/system/alpine# cat Dockerfile

FROM registry.cn-hangzhou.aliyuncs.com/qiushi/alpine:3.18.0
LABEL image="base"
#COPY repositories /etc/apk/repositories
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/' /etc/apk/repositories && \
    apk update && \
    apk --no-cache add tzdata gcc make curl zip unzip net-tools pstree wget libgcc \
    libc-dev libcurl libc-utils pcre-dev zlib-dev libnfs pcre pcre2 libevent \
    libevent-dev iproute2 && \ 
    ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
RUN apk --no-cache add openssl-dev 
#添加系统账户
RUN  addgroup -g 2024 -S www && \
     adduser -u 2024 -G www -D -S -s /sbin/nologin www 

root@ubuntu:~/system/alpine# cat build.sh

#/bin/bash
docker build -t registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0:$1  .

bash build.sh v1

docker push registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0:v1

nginx镜像多级构建【基于B、A】

准备nginx.conf文件

user  www;
worker_processes  auto;
...
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
.....
include conf.d/*.conf; 
}

root@ubuntu:~/web/nginx# cat Dockerfile

#多级构建,第1级:不产生新镜像
#1、nginx只编译部分,只是编译文件,并将编译文件传给下一个镜像镜像制作阶段
ARG VERSION=v1
FROM registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0:$VERSION
LABEL image="build"
ENV NGINX_VERSION=1.24.0
ENV NGINX_DIR=/apps/nginx
ADD nginx-$NGINX_VERSION.tar.gz /usr/local/src
RUN cd /usr/local/src/nginx-$NGINX_VERSION && \
    ./configure --prefix=${NGINX_DIR} --user=nginx --group=nginx \
    --with-http_ssl_module --with-http_v2_module --with-http_realip_module \
    --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream \
    --with-stream_ssl_module --with-stream_realip_module && \
    make && make install && \
    rm -rf /usr/local/src/nginx-$NGINX_VERSION
COPY nginx.conf ${NGINX_DIR}/conf/nginx.conf

#多级构建,第2级:产生新镜像
#1、将第一级构建部分cp过来
#2、采用最简洁的官方alpine镜像,没有额外包,镜像最终数据达到最小
FROM registry.cn-hangzhou.aliyuncs.com/qiushi/alpine:3.18.0
ENV NGINX_DIR=/apps/nginx
COPY --from=0 ${NGINX_DIR}/ ${NGINX_DIR}/
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/' /etc/apk/repositories \
   && apk update && apk --no-cache add tzdata pcre pcre2 \
   && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
   && echo "Asia/Shanghai" > /etc/timezone \
   && ln -sf ${NGINX_DIR}/sbin/nginx /usr/sbin/nginx \
   && addgroup -g 2024 -S www  \
   && adduser -u 2024 -G www -D -S -s /sbin/nologin www \
   && chown -R www.www ${NGINX_DIR}/ \
   && ln -sf /dev/stdout ${NGINX_DIR}/logs/access.log \
   && ln -sf /dev/stderr ${NGINX_DIR}/logs/error.log
EXPOSE 80 443
CMD ["nginx","-g","daemon off;"]

root@ubuntu:~/web/nginx# cat build.sh

#!/bin/bash
docker build -t registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-v1-nginx:$1  .
bash  build.sh  v1
docker push registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-v1-nginx:v1

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值