如何在Linux主机上的docker中运行avahi?

lg.*_*rom 6 avahi docker docker-compose

我正在尝试在 Linux 主机上运行的 docker 容器中设置 avahi。目的是让avahi公布一个自己的服务,让我找到docker主机的host和IP。

到目前为止,avahi 似乎在容器中运行良好,但我无法找到从主机外部搜索的服务。

我在谷歌上搜索了很多,并且有一些建议该怎么做,但它们似乎都是矛盾的和/或不安全的。

这是我到目前为止所得到的。

docker-compose.yml

version: "3.7"
services:
    avahi:
        container_name: avahi
        build:
            context: ./config/avahi
            dockerfile: DockerFile
            network: host
Run Code Online (Sandbox Code Playgroud)

Docker文件:

FROM alpine:3.13

RUN apk add --no-cache avahi avahi-tools
ADD avahi-daemon.conf /etc/avahi/avahi-daemon.conf
ADD psmb.service /etc/avahi/services/mpsu.service


ENTRYPOINT avahi-daemon --no-drop-root --no-rlimits
Run Code Online (Sandbox Code Playgroud)

avahi-daemon.conf:

[server]
enable-dbus=no
Run Code Online (Sandbox Code Playgroud)

psmb.service:(我的服务)

<!DOCTYPE service-group SYSTEM "avahi-service.dtd"> 
<service-group> 
    <name replace-wildcards="yes">PSMB</name>
    <service> <type>_mqtt._tcp</type> <port>1883</port>
        <txt-record>info=MPS Service Host</txt-record>
    </service>
</service-group>
Run Code Online (Sandbox Code Playgroud)

这是启动 avahi 时的终端:

> docker-compose up                                       
Starting avahi ... done
Attaching to avahi
avahi    | avahi-daemon 0.8 starting up.
avahi    | WARNING: No NSS support for mDNS detected, consider installing nss-mdns!
avahi    | Loading service file /etc/avahi/services/mpsu.service.
avahi    | Loading service file /etc/avahi/services/sftp-ssh.service.
avahi    | Loading service file /etc/avahi/services/ssh.service.
avahi    | Joining mDNS multicast group on interface eth0.IPv4 with address 172.18.0.2.
avahi    | New relevant interface eth0.IPv4 for mDNS.
avahi    | Joining mDNS multicast group on interface lo.IPv4 with address 127.0.0.1.
avahi    | New relevant interface lo.IPv4 for mDNS.
avahi    | Network interface enumeration completed.
avahi    | Registering new address record for 172.18.0.2 on eth0.IPv4.
avahi    | Registering new address record for 127.0.0.1 on lo.IPv4.
avahi    | Server startup complete. Host name is 8f220b5ac449.local. Local service cookie is 1841391818.
avahi    | Service "8f220b5ac449" (/etc/avahi/services/ssh.service) successfully established.
avahi    | Service "8f220b5ac449" (/etc/avahi/services/sftp-ssh.service) successfully established.
avahi    | Service "PSMB" (/etc/avahi/services/mpsu.service) successfully established.
Run Code Online (Sandbox Code Playgroud)

那么,我该如何配置才能搜索我的服务呢?
我想获取运行 docker 的主机的主机信息。

Bia*_*int 1

所以,我遇到了这个项目https://gitlab.com/ydkn/docker-avahi

Dockerfile:

# base image
ARG ARCH=amd64
FROM $ARCH/alpine:3

# args
ARG VCS_REF
ARG BUILD_DATE

# labels
LABEL maintainer="Florian Schwab <me@ydkn.io>" \
  org.label-schema.schema-version="1.0" \
  org.label-schema.name="ydkn/avahi" \
  org.label-schema.description="Simple Avahi docker image" \
  org.label-schema.version="0.1" \
  org.label-schema.url="https://hub.docker.com/r/ydkn/avahi" \
  org.label-schema.vcs-url="https://gitlab.com/ydkn/docker-avahi" \
  org.label-schema.vcs-ref=$VCS_REF \
  org.label-schema.build-date=$BUILD_DATE

# install packages
RUN apk --no-cache --no-progress add avahi avahi-tools

# remove default services
RUN rm /etc/avahi/services/*

# disable d-bus
RUN sed -i 's/.*enable-dbus=.*/enable-dbus=no/' /etc/avahi/avahi-daemon.conf

# entrypoint
ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT [ "docker-entrypoint.sh" ]

# default command
CMD ["avahi-daemon"]

# volumes
VOLUME ["/etc/avahi/services"]
Run Code Online (Sandbox Code Playgroud)

您必须更改/取消注释 /etc/avahi/avahi-daemon.conf 中的 ALLOW_INTERFACES 和 DENY_INTERFACES 以“允许”您正在使用的任何 eth 端口。IE

avahi-daemon.conf:

...
use-ipv4=yes
use-ipv6=yes
allow-interfaces=eth1 # was commented out with eth0
deny-interfaces=eth0 # was commented out with eth1
...
Run Code Online (Sandbox Code Playgroud)

然后您可以简单地运行容器,并与ALLOW_INTERFACES=???avahi-daemon.conf 中设置的内容相匹配

运行命令:

docker run -d --restart always \
  --net=host \
  -e ALLOW_INTERFACES=eth1 \
  -v $(pwd)/services:/etc/avahi/services \
  ydkn/avahi:latest

Run Code Online (Sandbox Code Playgroud)

似乎可以工作,我能够computername.local从另一台计算机上/连接到路由器进行 ping 操作,computername终端线路上的内容在哪里,即username@computername:~$

看起来还有一种方法可以添加安装的服务文件,/etc/avahi/services因此我相信您可以将服务名称自定义为其他更有用的名称。我需要弄清楚如何做到这一点,当我发现时将进行编辑。