Docker Container as Mininet Host
August 21, 2014 12 Comments
Docker seems to quite the buzz these days. It’s interesting what you could do with it, but I wanted try and see if it can help in any way when used in Mininet. A lot of times when you want to run certain applications inside a Mininet Host, you had to install that software on your PC/VM since the Mininet Host and your physical host share the same file system. This does come in handy a lot of times. But what if I don’t want to clutter up my physical host with extra apps. This seems like a great place for Docker to be used. Since Mininet uses veth pairs to connect the Mininet Host to the switches, it seemed that it wouldn’t be to hard to swap out the Mininet Host with a Docker host since Docker containers can also use veth pairs.
So I wrote a small test class to try it out. I wrote a new DockerHost class and added one to my topology.
h1 = net.addHost( 'h1', ip='10.0.0.1', cls=DockerHost ) h2 = net.addHost( 'h2', ip='10.0.0.2')
Then tried it out.
mininet@mininet:~$ sudo ./dockerHost.py *** Adding controller *** Adding hosts Removing any old host still running mininet-h1 mininet-h1 Start Docker Host *** Adding switch *** Creating links *** Starting network *** Configuring hosts h1 h2 *** Starting controller *** Starting 1 switches s1 *** Running CLI *** Starting CLI: mininet> pingall *** Ping: testing ping reachability h1 -> h2 h2 -> h1 *** Results: 0% dropped (2/2 received) mininet>
I was quite happy to see this work. The Docker host I used is a basic Ubuntu image. So it has no special apps in it. Mininet is just starting the Docker container as such.
docker run --priviledged -h h1 --name=mininet-h1 -ti --net='none' ubuntu /bin/bash
I can see it running here.
mininet@mininet:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a33e763eb709 ubuntu:latest /bin/bash 19 seconds ago Up 18 seconds mininet-h1
You can pick up the experiment and play with it here and most likely improve it. If you add applications, for the above to still work, the container needs to be started as a shell so Mininet can configure the networking inside and send other commands. So any applications that need to start automatically must be started internally, possible using the supervisord process.
A quick note/disclaimer that this is a bit of a hack still. It isn’t considered stable by any means since this was just the beginning of the experiment, but I wanted to share my success so far. So use at your own risk.
If you are able to improve it, please ping me back as I’d love to see what you did.
This post ‘Docker Container as Mininet Host’ first appeared on https://techandtrains.com/.
great works, would you mind sharing python code in creating docker host?
The link was provided in the post. But here it is.
https://app.box.com/s/ex3f2hbxdt4sjb3yuh1r
Hi, I was trying to run this example, however when it runs I get the following error:
*** Adding switch
*** Creating links
Traceback (most recent call last):
File “./dockerHost.py”, line 160, in
emptyNet()
File “./dockerHost.py”, line 147, in emptyNet
net.addLink( h1, s1 )
File “build/bdist.linux-x86_64/egg/mininet/net.py”, line 363, in addLink
File “build/bdist.linux-x86_64/egg/mininet/link.py”, line 416, in __init__
File “build/bdist.linux-x86_64/egg/mininet/link.py”, line 55, in __init__
File “build/bdist.linux-x86_64/egg/mininet/link.py”, line 188, in config
File “build/bdist.linux-x86_64/egg/mininet/link.py”, line 170, in setParam
File “build/bdist.linux-x86_64/egg/mininet/link.py”, line 131, in isUp
File “build/bdist.linux-x86_64/egg/mininet/link.py”, line 63, in ifconfig
File “build/bdist.linux-x86_64/egg/mininet/link.py”, line 59, in cmd
File “build/bdist.linux-x86_64/egg/mininet/node.py”, line 343, in cmd
File “./dockerHost.py”, line 65, in sendCmd
self.write( cmd + ‘\n’ )
File “build/bdist.linux-x86_64/egg/mininet/node.py”, line 235, in write
OSError: [Errno 32] Broken pipe
Any help will be valid!
What version of Mininet are you running this with? This class was written for Mininet 2.1. I have not test, and assume it is not compatible with 2.2+.
Hi, thanks for the post.
When I ran the dockerHost.py with python 2.7, i got several errors. The following is the trace of the run with some added printout.
The last error is actually fatal and it seems caused by the Popen(cmd, …) in the startShell() method:
[‘time=”2015-06-11T08:14:17-07:00″ level=”fatal” msg=”cannot enable tty mode on non tty input” \n’]
I also tried to add “-d” to the cmd to run the container in the detached mode. The above error went away, but then i got a “OSError: [Errno 32] Broken pipe” from the sendCmd() method.
Do you have any idea about the problems? Do you know any documents on how to extend mininet API?
many thanks.
*** Adding controller
*** Adding hosts
Error response from daemon: No such container: mininet-h1
time=”2015-06-11T08:14:17-07:00″ level=”fatal” msg=”Error: failed to stop one or more containers”
Error response from daemon: No such container: mininet-h1
time=”2015-06-11T08:14:17-07:00″ level=”fatal” msg=”Error: failed to remove one or more containers”
Removing any old host still running
Start Docker Host
[‘docker’, ‘run’, ‘–privileged’, ‘-h’, ‘h1’, ‘–name=mininet-h1’, ‘-it’, “–net=’none'”, ‘ubuntu’, ‘/bin/bash’]
[‘time=”2015-06-11T08:14:17-07:00″ level=”fatal” msg=”cannot enable tty mode on non tty input” \n’]
Traceback (most recent call last):
File “dockerHost.py”, line 164, in
emptyNet()
File “dockerHost.py”, line 143, in emptyNet
h1 = net.addHost( ‘h1′, ip=’10.0.0.1’, cls=DockerHost )
File “build/bdist.linux-x86_64/egg/mininet/net.py”, line 223, in addHost
File “dockerHost.py”, line 33, in __init__
Host.__init__( self, name, **kwargs )
File “build/bdist.linux-x86_64/egg/mininet/node.py”, line 101, in __init__
File “dockerHost.py”, line 131, in startShell
self.pid = int(ps_out[0])
ValueError: invalid literal for int() with base 10: ‘Error: No such image or container: mininet-h1\n’
docker_version
Client version: 1.5.0
Client API version: 1.17
Go version (client): go1.4.1
Git commit (client): a8a31ef
OS/Arch (client): linux/amd64
Server version: 1.5.0
Server API version: 1.17
Go version (server): go1.4.1
Git commit (server): a8a31ef
mininet_version
2.2.0
Hi,
I am getting the same error, did you manage to fix that :)
*** Adding controller
*** Adding hosts
Removing any old host still running
Error response from daemon: No such container: mininet-h1
Error response from daemon: No such container: mininet-h1
Start Docker Host
Traceback (most recent call last):
File “./dockerhost.py”, line 156, in
emptyNet()
File “./dockerhost.py”, line 135, in emptyNet
h1 = net.addHost( ‘h1’, cls=DockerHost)
File “build/bdist.linux-x86_64/egg/mininet/net.py”, line 224, in addHost
File “./dockerhost.py”, line 30, in __init__
Host.__init__( self, name, **kwargs )
File “build/bdist.linux-x86_64/egg/mininet/node.py”, line 106, in __init__
File “./dockerhost.py”, line 123, in startShell
self.pid = int(ps_out[0])
ValueError: invalid literal for int() with base 10: ”
i have the same error though my mininet`version is 2.1. please help me.
Hey everybody,
I am working on a full-featured implementation of a Docker container feature for Mininet. The solution is inspired by this code snippet shown here. However, to get things working in the latest Mininet version, more than the code snipped shown here is needed (e.g. to setup links to containers).
You can find the implementation here: https://github.com/mpeuster/dockernet
The code is still WIP Contributions are very welcome ;)
That looks great. This was written for an older version of Mininet and probably isn’t compatible with the latest Mininet. I’ll definitely be keeping an eye on your project.
The repository has moved: https://github.com/containernet/containernet
Pingback: Using Docker Containers as Mininet Hosts
Pingback: Guide On Using Docker Containers As Mininet Hosts | Talentica Blog