制作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