编译docker版openresty
使用alpine为基础镜像
# 使用Alpine作为基础镜像
FROM alpine:3.18# 替换为阿里云镜像源,并安装必要的依赖
RUN sed -i 's|https://dl-cdn.alpinelinux.org/alpine|https://mirrors.aliyun.com/alpine|g' /etc/apk/repositories && \apk add --no-cache \build-base \pcre-dev \openssl-dev \zlib-dev \perl \linux-headers \&& mkdir -p /usr/local/openresty# 下载OpenResty源码包
WORKDIR /tmp
RUN wget https://openresty.org/download/openresty-1.21.4.1.tar.gz && \tar zxpf openresty-1.21.4.1.tar.gz# 编译并安装OpenResty
WORKDIR /tmp/openresty-1.21.4.1
RUN ./configure --prefix=/usr/local/openresty \--with-pcre-jit \--with-http_ssl_module \--with-http_v2_module \--with-http_realip_module \--with-http_stub_status_module \-j2 && \make && \make install# 清理无用文件
RUN rm -rf /tmp/openresty-1.21.4.1*# 设置工作目录
WORKDIR /usr/local/openresty# 暴露端口
EXPOSE 80# 启动命令
CMD ["/usr/local/openresty/nginx/sbin/nginx", "-g", "daemon off;"]
上面编译完457M,多阶段构建可以缩减至25.7M
# stage 1: 构建阶段
FROM alpine:3.18 AS builder# 替换为阿里云源并安装构建依赖
RUN sed -i 's|https://dl-cdn.alpinelinux.org/alpine|https://mirrors.aliyun.com/alpine|g' /etc/apk/repositories && \apk add --no-cache \build-base \pcre-dev \openssl-dev \zlib-dev \perl \linux-headers# 下载并解压 OpenResty 源码
WORKDIR /tmp
RUN wget https://openresty.org/download/openresty-1.21.4.1.tar.gz && \tar zxpf openresty-1.21.4.1.tar.gz# 编译安装 OpenResty
WORKDIR /tmp/openresty-1.21.4.1
RUN ./configure --prefix=/usr/local/openresty \--with-pcre-jit \--with-http_ssl_module \--with-http_v2_module \--with-http_realip_module \--with-http_stub_status_module \-j2 && \make && \make install# 剥离可执行文件
RUN strip /usr/local/openresty/nginx/sbin/nginx# stage 2: 最终运行环境
FROM alpine:3.18# 安装运行时依赖
RUN sed -i 's|https://dl-cdn.alpinelinux.org/alpine|https://mirrors.aliyun.com/alpine|g' /etc/apk/repositories && \apk add --no-cache \libgcc \pcre \openssl \zlib# 复制编译好的 OpenResty 文件
COPY --from=builder /usr/local/openresty /usr/local/openresty# 设置工作目录
WORKDIR /usr/local/openresty# 暴露端口
EXPOSE 80# 启动命令
CMD ["/usr/local/openresty/nginx/sbin/nginx", "-g", "daemon off;"]
编译arm版docker
需要安装dockerbuildx
docker buildx create --use --name=mybuilder-cn --driver docker-container --driver-opt image=uhub.service.ucloud.cn/iatc/cn-buildkit:latest
docker buildx use mybuilder-cn
docker buildx build --platform linux/arm/v7 -t openresty-arm:latest . --load
docker buildx rm mybuilder-cn