1.下载nginx-1.26.2源码,并解压

nginx源码下载:https://nginx.org/download/nginx-1.26.2.tar.gz

mkdir dockerbuild && cd dockerbuild 

wget https://nginx.org/download/nginx-1.26.2.tar.gz

tar -xzvf nginx-1.26.2.tar.gz

2.干货Dockerfile

#编译基础环境准备阶段
FROM registry.cn-hangzhou.aliyuncs.com/mytest_docker123/debian:bookworm-slim  as base
# ENV NGINX_VERSION   1.26.2
RUN set -x \
# create nginx user/group first, to be consistent throughout docker variants
    #&& groupadd --system --gid 101 nginx \
   # && useradd --system --gid nginx --no-create-home --home /nonexistent --comment "nginx user" --shell /bin/false --uid 101 nginx \
    && sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list  \
    && sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && apt-get update \
    && apt-get install -y gcc  make  libpcre3 libpcre3-dev libssl-dev  libssl-dev  zlib1g-dev libxml2 libxslt-dev  libgd-dev libperl-dev



#编译阶段
FROM base as install-nginx
WORKDIR /opt
COPY nginx-1.26.2  nginx-1.26.2

RUN set -x \
    &&  cd nginx-1.26.2 \
    && chmod +x configure \
    && ./configure --prefix=/opt/nginx/ --user=nginx --group=nginx --with-compat --with-debug --with-file-aio  --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' \
    && make && make install

#安装打镜像
FROM registry.cn-hangzhou.aliyuncs.com/mytest_docker123/debian:bookworm-slim
COPY --from=install-nginx  /opt/nginx /opt/nginx
WORKDIR /opt/nginx
RUN set -x \
    && sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list  \
    && sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && apt-get update &&  apt-get install -y libssl3 \
    && groupadd --system --gid 101 nginx \
    && useradd --system --gid nginx --no-create-home --home /nonexistent --comment "nginx user" --shell /bin/false --uid 101 nginx


EXPOSE 80

ENTRYPOINT ["/bin/sh", "-c","/opt/nginx/sbin/nginx -g 'daemon off;'"]

3.构建镜像

docker build -t registry.cn-hangzhou.aliyuncs.com/mytest_docker123/nginx:1.26.2 .

4.测试运行

docker run -it -d -p 86:80 registry.cn-hangzhou.aliyuncs.com/mytest_docker123/nginx:1.26.2

5.验证