欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 编译docker版openresty

编译docker版openresty

2025/5/15 1:46:35 来源:https://blog.csdn.net/gsls200808/article/details/147930650  浏览:    关键词:编译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.21.3 AS builderARG OPENRESTY_VERSION=1.27.1.2# 替换为阿里云源并安装构建依赖
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-${OPENRESTY_VERSION}.tar.gz && \tar zxpf openresty-${OPENRESTY_VERSION}.tar.gz# 编译安装 OpenResty
WORKDIR /tmp/openresty-${OPENRESTY_VERSION}
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.21.3# 安装运行时依赖# 安装运行时依赖与时区支持
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 \tzdata && \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \echo "Asia/Shanghai" > /etc/timezone# 复制编译好的 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(10代i74c4g编译1小时)

需要安装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

在x86机器上运行armv7版

docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
docker run -it --platform=linux/arm/v7 openresty-arm:latest

docker版的helloworld

docker exec -it openresty sh
cd /usr/local/openresty/nginx/conf
#编辑nginx.conf 将location/内容替换为location / {default_type text/plain;content_by_lua_block {ngx.say("Hello World")}}
#然后保存
#最后重载配置
/usr/local/openresty/nginx/sbin/nginx -s reload

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词