基于alpine制作nginx镜像

1、修改dockerfile文件中的NGINX_VERSION变量可以更改nginx版本。
2、alpine安装语言包过程中的几个安装包地址是我自己指定的码云上的地址。
3、容器运行的用户为nginx,运行脚本为entrypoint.sh,可以自行修改。
4、默认修改了nginx.conf,未开启任何端口,可以将配置文件放在/opt/nginx/conf.d目录下,自定义端口。
5、构建过程中安装了busybox及busybox-extras两个工具包,不需要的话可以删掉,这样打出来的镜像应该更小。最终的nginx镜像只有55.6MB,如下图所示。


image.png

6、如果您觉得哪里有疑问请留言,共同交流进步,谢谢。

# ##### 此阶段下载本次构建必要文件 ##### #
FROM alpine:latest as glibc.download

# ARG GLIBC_URL="https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-2.35-r0.apk"
ARG SGERRAND_RUL="https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub"
# ARG GLIBC_BIN="https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-bin-2.35-r0.apk"
# ARG GLIBC_I18N="https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-i18n-2.35-r0.apk"

ARG GLIBC_URL="https://gitee.com/lizhip/alpine-pkg-glibc/attach_files/1031099/download/glibc-2.35-r0.apk"
ARG GLIBC_BIN="https://gitee.com/lizhip/alpine-pkg-glibc/attach_files/1031100/download/glibc-bin-2.35-r0.apk"
ARG GLIBC_I18N="https://gitee.com/lizhip/alpine-pkg-glibc/attach_files/1031101/download/glibc-i18n-2.35-r0.apk"
ARG NGINX_VERSION="1.20.2"
ARG GOSU_VERSION="1.14"

RUN set -x \
&& sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk --no-cache add ca-certificates tzdata wget busybox-extras \
&& mkdir /build \
&& cd /build \
&& wget -c -q -O /etc/apk/keys/sgerrand.rsa.pub ${SGERRAND_RUL} \
&& wget -c ${GLIBC_URL} && wget -c ${GLIBC_BIN} && wget -c ${GLIBC_I18N} && wget -c http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz \
&& wget -c https://github.com/tianon/gosu/releases/download/${GOSU_VERSION}/gosu-amd64 && mv gosu-amd64 gosu && chmod +x gosu \
&& echo -e '#!/bin/sh \n \
set -xe \n \
chown -R nginx:nginx /opt/nginx \n \
exec gosu nginx /bin/sh -c "/opt/nginx/sbin/nginx -p /opt/nginx" \n' \
> entrypoint.sh \
&& chmod +x entrypoint.sh

# ##### 此阶段配置及安装nginx ##### #
FROM alpine:latest as nginx.install
ARG NGINX_VERSION="1.20.2"
COPY --from=glibc.download /build /build
RUN set -x \
&& sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk --no-cache add ca-certificates gcc libc-dev make openssl-dev pcre-dev zlib-dev linux-headers curl gnupg libxslt-dev gd-dev geoip-dev \
&& cd /build \
&& tar -xvf nginx-${NGINX_VERSION}.tar.gz \
&& cd nginx-${NGINX_VERSION} \
&& ./configure --prefix=/opt/nginx --with-http_ssl_module \
&& make \
&& make install \
&& mkdir -p /opt/nginx/conf.d \
&& rm -f /opt/nginx/conf/nginx.conf && rm -rf /opt/nginx/html/* \
&& echo -e "worker_processes 8; \n \
events { \n \
worker_connections 65535; \n \
} \n \
daemon off; \n \
http { \n \
    include mime.types; \n \
    default_type application/octet-stream; \n \
    sendfile on; \n \
    keepalive_timeout 65; \n \
    server_tokens off; \n \
    tcp_nopush on; \n \
    tcp_nodelay on; \n \
    server_names_hash_bucket_size 128; \n \
    server_names_hash_max_size 512; \n \
    client_body_timeout 15s; \n \
    send_timeout 60s; \n \
    include /opt/nginx/conf.d/*.conf; \n \
    log_format log_json '{ "@timestamp": "$time_iso8601", ' \n \
                        '"remote_addr": "$remote_addr", ' \n \
                        '"referer": "$http_referer", ' \n \
                        '"request": "$request", ' \n \
                        '"status": $status, ' \n \
                        '"bytes": $body_bytes_sent, ' \n \
                        '"agent": "$http_user_agent", ' \n \
                        '"x_forwarded": "$http_x_forwarded_for", ' \n \
                        '"up_addr": "$upstream_addr",' \n \
                        '"up_host": "$upstream_http_host",' \n \
                        '"up_resp_time": "$upstream_response_time",' \n \
                        '"request_time": "$request_time"' \n \
                        ' }'; \n \
} \n" \
>/opt/nginx/conf/nginx.conf


# ##### 此阶段是最终需求的构建(得到一个包含中文语言包、中国标准时区的alpine-glibc-nginx镜像) ##### #
FROM alpine:latest

COPY --from=glibc.download /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY --from=glibc.download /etc/apk/keys/sgerrand.rsa.pub /etc/apk/keys/sgerrand.rsa.pub
COPY --from=glibc.download /build /build
COPY --from=glibc.download /bin/busybox-extras /bin/busybox-extras
COPY --from=glibc.download /bin/busybox /bin/busybox
COPY --from=nginx.install /opt/nginx /opt/nginx

RUN set -x \
&& sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& cd /build \
&& apk --no-cache add openssl-dev pcre-dev zlib-dev \
&& apk add glibc-2.35-r0.apk \
&& apk add glibc-bin-2.35-r0.apk \
&& apk add glibc-i18n-2.35-r0.apk \
&& gunzip /usr/glibc-compat/share/i18n/charmaps/UTF-8.gz \
&& ldconfig /lib /usr/glibc-compat/lib \
&& /usr/glibc-compat/bin/localedef -i zh_CN -f UTF-8 zh_CN.UTF-8 \
&& cp /build/gosu /usr/local/bin/ \
&& cp /build/entrypoint.sh /entrypoint.sh \
&& addgroup nginx \
&& adduser -G nginx -s /sbin/nologin -D nginx \
&& gosu nobody true \
&& rm -rf /build \
&& apk del glibc-i18n \
&& rm -rf /var/cache/apk/*

ENV PATH="/usr/glibc-compat/bin:$PATH" \
    LANG="zh_CN.UTF-8"
    
WORKDIR /opt/nginx
ENTRYPOINT ["/entrypoint.sh"]

你可能感兴趣的:(基于alpine制作nginx镜像)