[英]Trouble running UPNP on Docker
我尝试使用 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
否则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]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.