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

gfttwv5a  于 2023-01-29  发布在  Docker
关注(0)|答案(1)|浏览(354)

我正在尝试在Linux主机上运行的Docker容器中设置Avahi,目的是让Avahi宣布自己的一项服务,并形成我找到Docker主机的主机和IP。
到目前为止,avahi似乎在容器中运行良好,但我无法找到我的服务从我的主机外搜索。
我在谷歌上搜索了很多,有一些建议该怎么做,但它们似乎都是矛盾的和/或不安全的。
这是我目前为止得到的。
docker-compose.yml

version: "3.7"
services:
    avahi:
        container_name: avahi
        build:
            context: ./config/avahi
            dockerfile: DockerFile
            network: host

对接文件:

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

avahi-daemon.conf:

[server]
enable-dbus=no

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>

这是从终端启动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.

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

esbemjvw

esbemjvw1#

我偶然发现了这个项目https://gitlab.com/ydkn/docker-avahi
停靠文件:

# 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"]

你必须更改/取消注解/etc/avahi/avahi-daemon. conf中的ALLOW_INTERFACES和DENY_INTERFACES以"允许"你正在使用的任何以太网端口。
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
...

然后您可以简单地运行容器,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

看起来工作正常,我能够从路由器上/连接到路由器的另一台计算机ping computername.local,其中computername是终端线上的任何东西,iidoee. username@computername:~$
看起来也有一种方法来添加一个服务文件,挂载到/etc/avahi/services,所以我相信你可以自定义服务名称到其他更有用的东西。我需要弄清楚如何做到这一点,将编辑当我找到。

相关问题