You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Alpine镜像构建lua-openssl时遇strerror_s未定义引用错误求助

解决OpenResty Alpine镜像构建lua-openssl时的strerror_s未定义错误

问题描述

我尝试构建集成lua-openssl的OpenResty Alpine镜像,使用的Dockerfile如下:

FROM openresty/openresty:alpine-fat
# Set the version
ENV RESTY_CONFIG_OPTIONS_MORE "--with-ngx_http_ssl_module"
EXPOSE 80
EXPOSE 443
RUN ls /usr/local/openresty/nginx/logs
COPY lualib /usr/local/openresty/nginx/lualib
RUN chown -R nobody:root /usr/local/openresty/nginx/lualib
RUN apk add --update \
openssl openssl-dev \
lua5.3 luajit-dev lua-socket \
git
RUN git clone https://github.com/zhaozg/lua-openssl.git /usr/local/lua-openssl; \
cd /usr/local/lua-openssl; \
git checkout e923252b28cff43add6382853cc85ed888c4474b; \
make

但构建时出现大量类似如下的错误:

/usr/local/lua-openssl/deps/lua-compat/c-api/compat-5.3.c:74: undefined reference to strerror_s' ./libopenssl.a(cms.o): In function compat53_strerror':
/usr/local/lua-openssl/deps/lua-compat/c-api/compat-5.3.c:74: undefined reference to strerror_s' ./libopenssl.a(compat.o):/usr/local/lua-openssl/deps/lua-compat/c-api/compat-5.3.c:74: more undefined references to strerror_s' follow
collect2: error: ld returned 1 exit status
make: *** [Makefile:94: openssl.so] Error 1

请问是否缺少必要的依赖包?


错误原因分析

strerror_s是Windows平台专属的安全字符串函数,在Linux(包括Alpine使用的musl libc)环境中并不存在。你遇到的问题主要有两个原因:

  1. lua-openssl的这个版本默认启用了Windows兼容逻辑,编译时尝试链接不存在的strerror_s函数;
  2. Dockerfile缺少Linux下编译C代码必需的基础工具链。

解决方案

修改Dockerfile,补充编译工具并调整lua-openssl的编译参数,具体步骤如下:

  1. 添加编译工具依赖:在apk add命令中加入gcc、musl-dev、make,这些是编译C代码的基础工具;
  2. 禁用strerror_s相关逻辑:在make命令前设置环境变量USE_STRERROR_S=0,告诉lua-openssl使用Linux平台的等价函数替代Windows的strerror_s;
  3. 安装编译好的模块:编译完成后执行make install,将lua-openssl模块安装到OpenResty能识别的路径中;
  4. 优化镜像体积:添加--no-cache参数并清理安装缓存,编译完成后删除源码目录。

修改后的Dockerfile如下:

FROM openresty/openresty:alpine-fat
# Set the version
ENV RESTY_CONFIG_OPTIONS_MORE "--with-ngx_http_ssl_module"
EXPOSE 80
EXPOSE 443
RUN ls /usr/local/openresty/nginx/logs
COPY lualib /usr/local/openresty/nginx/lualib
RUN chown -R nobody:root /usr/local/openresty/nginx/lualib
RUN apk add --update --no-cache \
    openssl openssl-dev \
    lua5.3 luajit-dev lua-socket \
    git gcc musl-dev make \
    && rm -rf /var/cache/apk/*

RUN git clone https://github.com/zhaozg/lua-openssl.git /usr/local/lua-openssl; \
    cd /usr/local/lua-openssl; \
    git checkout e923252b28cff43add6382853cc85ed888c4474b; \
    USE_STRERROR_S=0 make \
    && USE_STRERROR_S=0 make install \
    && rm -rf /usr/local/lua-openssl

额外说明

  • OpenResty默认使用LuaJIT,已安装的luajit-dev依赖会确保lua-openssl自动适配LuaJIT环境;
  • 清理缓存和源码目录能有效减小最终镜像的体积,符合容器镜像轻量化的最佳实践。

内容的提问来源于stack exchange,提问作者dirtyqwerty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:17:44