在Alpine镜像构建lua-openssl时遇strerror_s未定义引用错误求助
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 functioncompat53_strerror':
/usr/local/lua-openssl/deps/lua-compat/c-api/compat-5.3.c:74: undefined reference tostrerror_s' ./libopenssl.a(compat.o):/usr/local/lua-openssl/deps/lua-compat/c-api/compat-5.3.c:74: more undefined references tostrerror_s' follow
collect2: error: ld returned 1 exit status
make: *** [Makefile:94: openssl.so] Error 1
请问是否缺少必要的依赖包?
错误原因分析
strerror_s是Windows平台专属的安全字符串函数,在Linux(包括Alpine使用的musl libc)环境中并不存在。你遇到的问题主要有两个原因:
- lua-openssl的这个版本默认启用了Windows兼容逻辑,编译时尝试链接不存在的
strerror_s函数; - Dockerfile缺少Linux下编译C代码必需的基础工具链。
解决方案
修改Dockerfile,补充编译工具并调整lua-openssl的编译参数,具体步骤如下:
- 添加编译工具依赖:在
apk add命令中加入gcc、musl-dev、make,这些是编译C代码的基础工具; - 禁用
strerror_s相关逻辑:在make命令前设置环境变量USE_STRERROR_S=0,告诉lua-openssl使用Linux平台的等价函数替代Windows的strerror_s; - 安装编译好的模块:编译完成后执行
make install,将lua-openssl模块安装到OpenResty能识别的路径中; - 优化镜像体积:添加
--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

