一. 案例

1.创建一个 container

  docker run -d --name test1 busybox /bin/sh -c "while true; do sleep 3600; done"

2.进入到 container 内

  docker exec -it test1 /bin/sh

3.查看 当前网络 ip a

  可以看出 其使用的 ip 地址为 127.0.0.1  和 172.17.0.2

4. 退出 到 虚拟机 使用 ip a

  可以看出 其 ip 地址 与 test1 内的 ip 地址不同  所以 通过 network Namespace 实现了网络的隔离

5. 再次创建相同的容器

  docker run -d --name test2 busybox /bin/sh -c "while true; do sleep 3600; done"

6.进入 查看新 容器的 IP

  docker exec test2 ip a

  可以看出 其使用的 ip 地址为 127.0.0.1  和 172.17.0.3

7. 进入 test2 容器 并 ping 通 test1 是可以 ping 通的

  docker exec -it test1 /bin/sh

  ping 172.17.0.2

二. network Namespace 操作

  1. 查看所有 network Namespace

    sudo ip netns list

  2. 删除 network Namespace

    sudo ip netns delete [ list 出来的项 ]

  3. 添加  network Namespace

    sudo ip netns add [eg:test1]

  4.查看创建的 容器 test1 的 ip

    sudo ip netns exec test1 ip a  在 test1 容器内 执行 ip a 命令

    sudo ip netns exec test1 ip link   在 test1 容器内 执行 ip link 命令

    sudo ip netns exec test1 ip link set dev lo up 让 test1 容器内 的 lo 回管口 up 起来

三 连接两个容器

  

  1. 添加一对link  (即 图中 那根管道)

    sudo ip link add veth-test1 type veth peer name veth-test2

    使用 ip a 查看创建的 link

      会出现 veth-test2@veth-test1   和  veth-test1@veth-test2 这一对link

  2. 将 veth-test1 添加到 test1 容器中  并且 将 veth-test2 添加到 test2 容器中

    sudo ip link set veth-test1 netns test1

    sudo ip netns exec test1 ip link   查看是否包含 veth-test1

    ip a                 查看包含的 veth 是否还包含 veth-test1

    sudo ip link set veth-test2 netns test2

    sudo ip netns exec test2 ip link  查看是否包含 veth-test2

    ip a                 查看包含的 veth 是否还包含 veth-test2

  3. 分配 ip 地址

    分配 ip 地址 test1

      sudo ip netns exec test1 ip addr add 192.168.1.13/24 dev veth-test1

    分配 ip 地址 test2

      sudo ip netns exec test2 ip addr add 192.168.1.14/24 dev veth-test2

    使用 ip link 查看是否分配 ip 地址  发现 网络 还未开启

      sudo ip netns exec test1 ip link

    将 test1 和 test2 的网络 开启

      sudo ip netns exec test1 ip link set dev veth-test1 up

      sudo ip netns exec test2 ip link set dev veth-test2 up

    使用 ip a 查看是否 分配ip 地址

      sudo ip netns exec test1 ip a

      sudo ip netns exec test2 ip a

    使用 ping 互通 test1 test2

      sudo ip netns exec test1 ping 192.168.1.14

      sudo ip netns exec test2 ping 192.168.1.13

13.docker 网络 docker NameSpace (networkNamespace)的更多相关文章

  1. 14. docker 网络 docker bridge0 详解

    1.创建一个 container docker run -d --name test1 busybox /bin/sh -c "while true; do sleep 3600; done ...

  2. 15 docker 网络 docker 容器之间的关系 docker link

    1.案例:使用 link 关联后台与数据库 创建 test1 容器 docker run -d --name test1 busybox /bin/sh -c "while true; do ...

  3. 12. docker 网络 docker network (docker 网络)

    1. 环境准备 编写 Vagrantfile 为 # -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.require_version "> ...

  4. 理解Docker :Docker 网络

    本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  5. (转)Docker 网络

    转:https://www.cnblogs.com/allcloud/p/7150564.html 本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker ...

  6. Docker 网络原理

    引言 学习docker网络,可以带着下面两个问题来探讨 容器之间可以相互访问的原理 容器暴露端口后,通过宿主机访问到容器内应用,并且对于访问端而言不用感知容器存在的原理 Docker 本身的技术依赖L ...

  7. 理解Docker(5):Docker 网络

    本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  8. docker网络

    docker网络   Docker 允许通过外部访问容器或容器互联的方式来提供网络服务. 端口映射允许外部访问容器 --link 容器互联 容器桥接网络 .通过--link容器通信,给test2添加一 ...

  9. 16.2,docker网络

      Docker 允许通过外部访问容器或容器互联的方式来提供网络服务. 端口映射允许外部访问容器 --link 容器互联 容器桥接网络 .通过--link容器通信,给test2添加一个hosts解析记 ...

随机推荐

  1. vue学习(二)Vue常用指令

    2 Vue常用指令 1. vue的使用要从创建Vue对象开始 var vm = new Vue(); 2. 创建vue对象的时候,需要传递参数,是json对象,json对象对象必须至少有两个属性成员 ...

  2. Junit学习使用

    InputStream in; SqlSessionFactory factory; SqlSession session; UserDao userDao; @BeforeEach public v ...

  3. 安装与配置windbg的symbol(符号)

    http://msdn.microsoft.com/en-us/windows/hardware/gg463028.aspx  windows symbols下载地址 本篇是新手自己写的一点心得.建议 ...

  4. 文献阅读报告 - Social GAN: Socially Acceptable Trajectories with Generative Adversarial Networks

    paper:Gupta A , Johnson J , Fei-Fei L , et al. Social GAN: Socially Acceptable Trajectories with Gen ...

  5. SrpingMVC/SpringBoot中restful接口序列化json的时候使用Jackson将空字段,空字符串不传递给前端

    笔者的JSON如下: { "code": 10001, "message": "成功", "nextUrl": null ...

  6. 高级css效果

    1.图片渐变效果 background linear-gradient(top,rgba(0,0,0,.8),rgba(0,0,0,.8))

  7. CodeForces - 748E Santa Claus and Tangerines(二分)

    题意:将n个蛋糕分给k个人,要保证每个人都有蛋糕或蛋糕块,蛋糕可切, 1.若蛋糕值为偶数,那一次可切成对等的两块. 2.若蛋糕值为奇数,则切成的两块蛋糕其中一个比另一个蛋糕值多1. 3.若蛋糕值为1, ...

  8. Sequence Models Week 1 Character level language model - Dinosaurus land

    Character level language model - Dinosaurus land Welcome to Dinosaurus Island! 65 million years ago, ...

  9. POJ 1006:Biorhythms 中国剩余定理

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 121194   Accepted: 38157 Des ...

  10. POJ 1519:Digital Roots

    Digital Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25766   Accepted: 8621 De ...