一、下载(pull)EMQX镜像
docker pull emqx/emqx
二、创建EMQX容器
docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8883:8883 -p 8084:8084 -p 18083:18083 --restart=always emqx/emqx
#创建一个名为emqx的容器,并将emqx的端口映射到主机上的相应端口。1883用于MQTT,8083用于MQTT over WebSocket,8883用于MQTT over SSL/TLS,8084用于MQTT over TCP/TLS,18083用于EMQ X Dashboard。
三、检查容器是否成功运行
docker ps
or
docker ps -a
①.成功
②.失败
四、EMQX 获取客户端上下线状态
(一)、复制acl.conf到宿主机
docker cp <容器名称或容器ID>:/opt/emqx/etc/acl.conf acl.conf
例如:docker cp emqx:/opt/emqx/etc/acl.conf acl.conf
(二)、编写宿主机acl.conf文件
①.进去acl.conf文件编辑
vim acl.conf
②.修改acl.conf文件
复制一下内容或添加{allow, all, subscribe, ["$SYS/brokers/+/clients/#"]}.
%%--------------------------------------------------------------------
%% -type(ipaddr() :: {ipaddr, string()}).
%%
%% -type(ipaddrs() :: {ipaddrs, [string()]}).
%%
%% -type(username() :: {user | username, string()} | {user | username, {re, regex()}}).
%%
%% -type(clientid() :: {client | clientid, string()} | {client | clientid, {re, regex()}}).
%%
%% -type(who() :: ipaddr() | ipaddrs() | username() | clientid() |
%% {'and', [ipaddr() | ipaddrs() | username() | clientid()]} |
%% {'or', [ipaddr() | ipaddrs() | username() | clientid()]} |
%% all).
%%
%% -type(action() :: subscribe | publish | all).
%%
%% -type(topic_filters() :: string()).
%%
%% -type(topics() :: [topic_filters() | {eq, topic_filters()}]).
%%
%% -type(permission() :: allow | deny).
%%
%% -type(rule() :: {permission(), who(), action(), topics()} | {permission(), all}).
%%--------------------------------------------------------------------
{allow, {username, {re, "^dashboard$"}}, subscribe, ["$SYS/#"]}.
{allow, {ipaddr, "127.0.0.1"}, all, ["$SYS/#", "#"]}.
{allow, all, subscribe, ["$SYS/brokers/+/clients/#"]}.
{deny, all, subscribe, ["$SYS/#", {eq, "#"}]}.
{allow, all}.
(三)、复制acl.conf到Docker
docker cp acl.conf <容器名称或容器ID>:/opt/emqx/etc/acl.conf
例如:docker cp acl.conf emqx:/opt/emqx/etc/acl.conf
(四)、重启EMQX容器
docker restart <容器名称或容器ID>
例如,docker restart emqx or docker restart b85f3084b39c
最后,重复第三步查询是否启动成功