---
# System prepended metadata

title: Mininet-container(dockernet)

---

# Mininet-container(dockernet)


切換到Container Net Env


## Enable Env

```
cd /home/user/containernet
python3 ./setup.py install
```


## Dockernet 抓 ubuntu:16:04(img檔)

```
docker pull ubuntu:16.04
```


```
root@ubuntu:/home/user/containernet# docker images
REPOSITORY                TAG       IMAGE ID       CREATED         SIZE
nginx                     latest    6efc10a0510f   4 weeks ago     142MB
ubuntu                    16.04     b6f507652425   20 months ago   135MB
ubuntu                    sshd2     824381f7a739   2 years ago     221MB
ubuntu                    sshd1     f71d6887c4b3   2 years ago     221MB
ubuntu                    <none>    f6f49faac5cf   2 years ago     132MB
kathara/quagga            latest    68a8736f634a   2 years ago     927MB
ubuntu                    trusty    df043b4f0cf1   2 years ago     197MB
smallko/php-apache-dev    v10       e365c329ad7e   4 years ago     1.32GB
cslev/p4c                 latest    60f0deb82f1e   4 years ago     3.89GB
cslev/p4-bmv2-p4runtime   full      1e4192083ed9   4 years ago     6.51GB
```

ubuntu 16.04 ->　Image File 
(這樣創出兩個 virtual host 來setting  不同的Account 跟 Password )

## Establish Container 
```
root@ubuntu:/home/user/containernet# docker run -it ubuntu:16.04 bash
root@609a868181a6:/# apt update
```

docker run 

->Establish and Execute Container 

docker start 

->Execute Container 

-it (options 不知道在幹嘛XD) 
bash -> 應該是 CLI mode

---

## 設定 SSHD server 的配置

ifconfig

```
root@22ba92d22e5f:/# apt-get install net-tools
```

ping 

```
root@22ba92d22e5f:/# apt-get install -y iputils-ping
```


Install ssh-server (dockerHost)

```
apt install openssh-server -y
```


New User Account 

```
adduser meowhecker #New User Account 
```

meow
meowps

這裡已經設定好 第一個 ssh host 配置

```
root@ubuntu:/home/user# docker ps
CONTAINER ID   IMAGE          COMMAND   CREATED          STATUS          PORTS     NAMES
609a868181a6   ubuntu:16.04   "bash"    13 minutes ago   Up 13 minutes             gracious_bhabha
```

創建 ubuntu:sshd1(也就是配好的ssh Server) image file 

```
root@ubuntu:/home/user# docker commit 609 ubuntu:sshd1
sha256:4e331f98c60dc2443f85c94748e20ff26a4c9ea285be85602c0eb27128f2b1a7

```

```
root@ubuntu:/home/user# docker images
REPOSITORY                TAG       IMAGE ID       CREATED         SIZE
ubuntu                    sshd1     4e331f98c60d   9 seconds ago   221MB
nginx                     latest    6efc10a0510f   4 weeks ago     142MB

```

---
第二台 ssh server 

```
root@609a868181a6:/# passwd meow
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
```
meow
meow2

```
root@ubuntu:/home/user# docker commit 609 ubuntu:sshd2 
sha256:846fe0e1d1908392a644ea04a109bb03011a220cfe6caccd2e6df0d9dd07736a
```
```
root@ubuntu:/home/user# docker images
REPOSITORY                TAG       IMAGE ID       CREATED         SIZE
ubuntu                    sshd2     846fe0e1d190   9 seconds ago   221MB
ubuntu                    sshd1     4e331f98c60d   6 minutes ago   221MB
```


## 砍掉Image 方法

```
docker rmi ubuntu:sshd1  #<REPOSITORY:TAG>
```
rm -> remove 意思
i -> ID (According ID 去做Delete)



Note 如果遇到 dockernet Run 不起來
可能是 container Name 衝突 引發


```
docker.errors.APIError: 409 Client Error: Conflict ("Conflict. The container name "/mn.d1" is already in use by container "52d730db7e79f329b50fd967c75ed405ab0e99b7763b9da8d5c1282b2650dc90". You have to remove (or rename) that container to be able to reuse that name.")
```

Solve way 

```
docker rm "<container ID>"
```
如果 container 已經run起來 也要停掉

```
docker stop "<container ID>"
```

Run dockernet


Topology


![](https://hackmd.io/_uploads/rk_pALJB2.png)



```=python
#!/usr/bin/python
"""
This is the most simple example to showcase Containernet.
"""
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
setLogLevel('info')

net = Containernet(controller=Controller)
info('*** Adding controller\n')
net.addController('c0')
info('*** Adding docker containers\n')
h1 = net.addHost('h1', ip='10.0.0.250/24')
d1 = net.addDocker('d1', ip='10.0.0.251/24', dimage="ubuntu:ssh1")
d2 = net.addDocker('d2', ip='10.0.0.252/24', dimage="ubuntu:ssh2")
info('*** Adding switches\n')
s1 = net.addSwitch('s1')
info('*** Creating links\n')
net.addLink(h1, s1)
net.addLink(d1, s1)
net.addLink(d2, s1)
info('*** Starting network\n')
net.start()
info('*** Running CLI\n')
CLI(net)
info('*** Stopping network')
net.stop()

```

---


```
root@ubuntu:/home/user/containernet# docker ps
CONTAINER ID   IMAGE         COMMAND       CREATED         STATUS         PORTS     NAMES
f26756cf7214   ubuntu:ssh2   "/bin/bash"   7 seconds ago   Up 6 seconds             mn.d2
83e81b45cedd   ubuntu:ssh1   "/bin/bash"   8 seconds ago   Up 7 seconds             mn.d1

```

## 進入container 方法(ssh1)

```
docker exec -it mn.d1 bash
```

```
root@ubuntu:/home/user/containernet-test# docker exec -it mn.d1 /bin/bash
root@d1:/#
```


Start SSH Service 

```
root@d1:/# ifconfig d1-eth0 10.0.0.251/24 #設定IP
root@d1:/# /etc/init.d/ssh start
 * Starting OpenBSD Secure Shell server sshd                             [ OK ] 
```

這裡會設 ip 是因為 Script 沒設到

---
看SSh 有沒有跑起來
```
root@d1:/# netstat -tunlp | grep 22
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      68/sshd         
tcp6       0      0 :::22                   :::*                    LISTEN      68/sshd         
```


ssh2 都次一樣操作

```
root@ubuntu:/home/user/containernet-test# docker exec -it mn.d2 bash
root@d2:/# ifconfig d2-eth0 10.0.0.252/24
root@d2:/# /etc/init.d/ssh start
 * Starting OpenBSD Secure Shell server sshd                             [ OK ] 
root@d2:/# 

```


![](https://hackmd.io/_uploads/SJAHuIyHh.png)

因為有 knowhost file(之前登入的帳密) -> 會導致 驗證失敗

```
root@ubuntu:~# cd .ssh/
root@ubuntu:~/.ssh# rm -r known_hosts 

```


```
meow@d1:~$ ssh meow@10.0.0.251
The authenticity of host '10.0.0.251 (10.0.0.251)' can't be established.
ECDSA key fingerprint is SHA256:rRPBZf+7qFn0e9OKmxUuY7q0zzmfOZeeoXuYoiMWVi8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.251' (ECDSA) to the list of known hosts.
meow@10.0.0.251's password: 
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-210-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
Last login: Mon May 15 07:02:47 2023 from 10.0.0.250
meow@d1:~$ 
```

```
root@ubuntu:~/.ssh# ssh meow2@10.0.0.252
The authenticity of host '10.0.0.252 (10.0.0.252)' can't be established.
ECDSA key fingerprint is SHA256:rRPBZf+7qFn0e9OKmxUuY7q0zzmfOZeeoXuYoiMWVi8.
Are you sure you want to continue connecting (yes/no)? yes 
Warning: Permanently added '10.0.0.252' (ECDSA) to the list of known hosts.
meow2@10.0.0.252's password: 
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-210-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

meow2@d2:~$  
```

這樣就成功登入了 sshd1 sshd2 

