gpt4 book ai didi

java - 在 Docker 上运行 UPNP 时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:47:33 26 4
gpt4 key购买 nike

我尝试使用 Cling UPNP 库 ( http://4thline.org/projects/cling/ ) 在我的 docker 容器上运行 UPnP 服务。有一个简单的程序可以创建一个托管某些服务的设备(在软件中)。这是用 Java 编写的,当我尝试运行该程序时,出现以下异常(注意:这直接在我的 Ubuntu 机器上运行得很好):

Jun 5, 2015 1:47:24 AM org.teleal.cling.UpnpServiceImpl <init>
INFO: >>> Starting UPnP service...
Jun 5, 2015 1:47:24 AM org.teleal.cling.UpnpServiceImpl <init>
INFO: Using configuration: org.teleal.cling.DefaultUpnpServiceConfiguration
Jun 5, 2015 1:47:24 AM org.teleal.cling.transport.RouterImpl <init>
INFO: Creating Router: org.teleal.cling.transport.RouterImpl
Exception occured: org.teleal.cling.transport.spi.InitializationException: Could not discover any bindable network interfaces and/or addresses
org.teleal.cling.transport.spi.InitializationException: **Could not discover any bindable network interfaces and/or addresses
at org.teleal.cling.transport.impl.NetworkAddressFactoryImpl.<init>(NetworkAddressFactoryImpl.java:99)**

最佳答案

对于任何发现此内容并需要答案的人。

您的容器正在遮挡您的外部网络。 换句话说,默认情况下容器是隔离的,无法看到外部网络,这当然是打开 IGD 中的端口所必需的。

您可以将容器作为“主机”运行以使其非隔离,只需将 --network host 添加到容器创建命令中

示例(取自 https://docs.docker.com/network/network-tutorial-host/#procedure ):

docker run --rm -d --network host --name my_nginx nginx

我已经使用 docker-compose.yml 对此进行了测试,它看起来有点不同:

version: "3.4"
services:
upnp:
container_name: upnp
restart: on-failure:10
network_mode: host # works while the container runs
build:
context: .
network: host # works during the build if needed

3.4版本非常重要,否则network:host将无法工作!

我的 upnp 容器 Dockerfile 看起来像这样:

FROM alpine:latest

RUN apk update
RUN apk add bash
RUN apk add miniupnpc

RUN mkdir /scripts
WORKDIR /scripts
COPY update_upnp .
RUN chmod a+x ./update_upnp

# cron to update each UPnP entries every 10 minutes
RUN echo -e "*/10\t*\t*\t*\t*\tbash /scripts/update_upnp 8080 TCP" >> /etc/crontabs/root

CMD ["crond", "-f"]

# on start, open needed ports
ENTRYPOINT bash update_upnp 80 TCP && bash update_upnp 8080 TCP

update_upnp 脚本只是使用 upnpc (在上面的 Dockerfile 中作为 miniupnpc 安装)来打开端口。

希望这会对某人有所帮助。

编辑:update_upnp 脚本如下所示:

#!/bin/bash

port=$1
protocol=$2
echo "Updating UPnP entry with port [$port] and protocol [$protocol]"

gateway=$(ip route | head -1 | awk '{print $3}')
echo "Detected gateway is [$gateway]"

# port - e.g. 80
# protocol - TCP or UDP
upnpc -e 'your-app-name' -r $port $protocol

echo "Done updating UPnP entry with port [$port] and protocol [$protocol]"

关于java - 在 Docker 上运行 UPNP 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30677702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com