Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

python - How to expose mininet (containernet) host ports to launch application on localhost?

Below is the topology I am trying to create with mininet(containernet) where there are 3 containers with a layer 2 switch in between. I want to access these containers (eg. kali linux container) on the localhost on the browser. I tried exposing the ports like it says in the documentation but still I cannot access it on the browser. Note- I can access the containers (eg kali linux conatiner) if I dont use containernet on localhost. For reference 3rd host uses the following docker container- https://hub.docker.com/r/lukaszlach/kali-desktop

#!/usr/bin/python

from mininet.net import Containernet
from mininet.node import Controller
from mininet.cli import CLI
from mininet.link import TCLink
from mininet.log import info, setLogLevel
#VNC_DEFAULT = 5900
#WEB_DEFAULT = 6080
setLogLevel('info')

net = Containernet(controller=Controller)
info('*** Adding controller
')
net.addController('c0' )
info('*** Adding docker containers
')
d1 = net.addDocker('d1', ip='10.0.2.16', dimage="virtualmogli_v1_service1", ports=[5000], port_bindings={5000:5000},publish_all_ports=True)
d2 = net.addDocker('d2', ip='10.0.2.15', dimage="virtualmogli_v1_service2", ports=[5001], port_bindings={5001:5001},publish_all_ports=True)
d3 = net.addDocker('d3', ip='10.0.2.17', dimage="virtualmogli_v1_kalicontainer", ports=[5900, 6080], port_bindings={5900: 5900, 6080: 6080}, publish_all_ports=True,)
info('*** Adding switches
')
s1 = net.addSwitch('s1')
#s2 = net.addSwitch('s2')
info('*** Creating links
')
net.addLink(d1, s1, cls=TCLink, delay='100ms', bw=1)
#net.addLink(s1, s2, cls=TCLink, delay='100ms', bw=1)
net.addLink(s1, d2, cls=TCLink, delay='100ms', bw=1)
net.addLink(s1, d3, cls=TCLink, delay='100ms', bw=1)
info('*** Starting network
')
net.start()
info('*** Testing connectivity
')
net.ping([d1, d2, d3])
info('*** Running CLI
')
CLI(net)
info('*** Stopping network')
net.stop()
question from:https://stackoverflow.com/questions/65902549/how-to-expose-mininet-containernet-host-ports-to-launch-application-on-localho

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The missing piece of the puzzle is we need to add dcmd="/init" to our containernet topology as opposed to our base class.

For above problem, the following code will work- d2 = net.addDocker('d2', ip='10.0.2.15', dimage="lukaszlach/kali-desktop:xfce-top10",ports=[5900,6080],dcmd="/init , port_bindings={5900:5900,6080:6080},publish_all_ports= True, environment={"USER": "kali", "PASSWORD": "kali" , "ROOT_PASSWORD": "root"})


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...