Dockerfile文件构建vernemq镜像,自定义init.lua脚本
-- init.lua 注册版
print('[AUTH] init.lua loaded')
-- 1. 定义钩子
local function auth_on_register(reg)
print('[AUTH] auth_on_register called')
print('[AUTH] username=' .. (reg.username or 'nil'))
print('[AUTH] client_id=' .. (reg.client_id or 'nil'))
return true -- 先无脑放过,调通后再加 SQL
end
local function auth_on_publish(pub)
return true
end
local function auth_on_subscribe(sub)
return true
end
-- 2. 必须把表赋给 **全局变量 hooks**
hooks = {
auth_on_register = auth_on_register,
auth_on_publish = auth_on_publish,
auth_on_subscribe= auth_on_subscribe
}
然后我使用Docker创建容器,使用mqttx连接,一直连接不上,mqttx报错Error: Connection refused: Bad User Name or Password,容器日志输出
2025-11-17T08:17:52.423367+00:00 [warning] <0.561.0> vmq_mqtt5_fsm:check_user/3:1119: can't authenticate client {[],<<"MQTTX-Monitor">>} from 172.17.0.1:53456 due to plugin_chain_exhausted
2025-11-17T08:17:52.423832+00:00 [debug] <0.561.0> vmq_ranch:teardown/2:190: session normally stopped
2025-11-17T08:17:52.662802+00:00 [debug] <0.270.0> vmq_swc_store:handle_info/2:799: Replica meta6: Can't initialize AE exchange due to no peer available
真的没办法了,脚本也加载了,一直找不到问题在哪,创建容器的命令
docker run -it --rm --name vernemq -p 10.0.2.72:1883:1883 -p 10.0.2.72:8883:8883 -p 10.0.2.72:8080:8080 -p 10.0.2.72:8081:8081 -p 10.0.2.72:44053:44053 -v D:\VerneMQ\vernemq.conf:/vernemq/etc/vernemq.conf -v D:\VerneMQ\vmq_diversity.conf:/vernemq/etc/vmq_diversity.conf -v D:\VerneMQ\init.lua:/vernemq/share/lua/auth/auth.lua -v D:\VerneMQ\vm.args:/vernemq/etc/vm.args vernemq-mysql:1.1 /vernemq/bin/vernemq console
两种挂载init.lua的方式都试过了,都不行
docker run -it --rm --name vernemq -p 10.0.2.72:1883:1883 -p 10.0.2.72:8883:8883 -p 10.0.2.72:8080:8080 -p 10.0.2.72:8081:8081 -p 10.0.2.72:44053:44053 -v D:\VerneMQ\vernemq.conf:/vernemq/etc/vernemq.conf -v D:\VerneMQ\vmq_diversity.conf:/vernemq/etc/vmq_diversity.conf -v D:\VerneMQ\init.lua:/vernemq/share/lua/init.lua -v D:\VerneMQ\vm.args:/vernemq/etc/vm.args vernemq-mysql:1.1 /vernemq/bin/vernemq console
vernemq.conf配置文件:
## ========= 基础 =========
allow_anonymous = off
allow_register_during_netsplit = on
allow_publish_during_netsplit = on
allow_subscribe_during_netsplit = on
allow_unsubscribe_during_netsplit = on
## ========= 监听 =========
listener.tcp.default = 0.0.0.0:1883
listener.ws.default = 0.0.0.0:8080
## ========= 日志 =========
log.console = console
log.console.level = debug
## ========= 插件 =========
plugins.vmq_diversity = on
vmq_diversity.script_dir = /vernemq/share/lua
Dockerfile文件内容:
# Stage 1: The builder stage. This container is used to build the VerneMQ application.
FROM ubuntu:24.04 AS builder
# 使用国内镜像源替换默认源
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list && \
sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
# 为 apt 设置不使用代理
RUN echo 'Acquire::http::Proxy "false";' > /etc/apt/apt.conf.d/99proxy && \
echo 'Acquire::https::Proxy "false";' >> /etc/apt/apt.conf.d/99proxy
# Install necessary build dependencies (不使用代理)
RUN apt-get update && apt-get install -y \
git \
build-essential \
cmake \
rebar3 \
erlang-dev \
erlang-parsetools \
erlang-tools \
libssl-dev \
libsnappy-dev \
procps \
libncurses5-dev \
&& rm -rf /var/lib/apt/lists/*
# 设置代理用于 GitHub 下载
ENV http_proxy=http://x.x.x.x
ENV https_proxy=http://x.x.x.x
ENV HTTP_PROXY=http://x.x.x.x
ENV HTTPS_PROXY=http://x.x.x.x
# Clone the VerneMQ repository, specifically the 2.1.1 tag.
RUN git clone -b 2.1.1 --depth 1 https://github.com/vernemq/vernemq.git /vernemq
# Set the working directory to the cloned repository.
WORKDIR /vernemq
# Build the VerneMQ release.
RUN make rel
# Stage 2: The final runtime image.
FROM ubuntu:24.04
# 使用国内镜像源替换默认源
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list && \
sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
# 为 apt 设置不使用代理
RUN echo 'Acquire::http::Proxy "false";' > /etc/apt/apt.conf.d/99proxy && \
echo 'Acquire::https::Proxy "false";' >> /etc/apt/apt.conf.d/99proxy
# 安装 Erlang 运行时和必要的依赖 (不使用代理)
RUN apt-get update && apt-get install -y \
erlang \
libssl-dev \
libsnappy1v5 \
default-mysql-client \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Create necessary directories
RUN mkdir -p /vernemq/etc /vernemq/share/lua
# Copy the compiled release from the builder stage.
COPY --from=builder /vernemq/_build/default/rel/vernemq /vernemq
# Copy configuration files
COPY vernemq.conf /vernemq/etc/vernemq.conf
COPY vmq_diversity.conf /vernemq/etc/vmq_diversity.conf
COPY mysql_auth.lua /vernemq/share/lua/mysql_auth.lua
RUN rm -f /vernemq/share/lua/auth/*mysql*.lua
# Set the working directory for the final image.
WORKDIR /vernemq
# 清除代理设置(运行时不需要)
ENV http_proxy=
ENV https_proxy=
ENV HTTP_PROXY=
ENV HTTPS_PROXY=
# Expose the standard VerneMQ ports.
EXPOSE 1883 8883 8080 8081 44053
# The default command to run when the container starts.
CMD ["/vernemq/bin/vernemq", "console"]