1. /*
  2.  
  3. 1. host模式 :
  4.  
  5. docker run 使用 --net=host指定
  6.  
  7. docker使用的网络实际上和宿主机一样
  8.  
  9. 2. container模式:
  10.  
  11. 使用 --net=container:container_id/container_name
  12.  
  13. 多个容器使用共同的网络,看到的ip是一样的。
  14.  
  15. 3. none 模式
  16.  
  17. 使用 --net=none指定
  18.  
  19. 这种模式下,不会配置任何网络。
  20.  
  21. 4. bridge模式
  22.  
  23. 使用 --net=bridge指定
  24.  
  25. 默认模式,不会指定
  26.  
  27. 此模式会为每个容器分配一个独立的network namespace
  28. */
  1. /* 外部网络访问容器 :外部的用户要访问容器,先将容器的ip映射出去,然后客户利用宿主机的ip来访问*/
  2. [root@30c1fec5df6a /]# yum install -y httpd
  3. //虽然报错,但是httpd已经启动
  4. [root@30c1fec5df6a /]# /usr/sbin/httpd
  5. AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
  6. [root@30c1fec5df6a /]# ps aux|grep httpd
  7. root 0.1 0.3 ? Ss : : /usr/sbin/httpd
  8. apache 0.0 0.2 ? S : : /usr/sbin/httpd
  9. apache 0.0 0.2 ? S : : /usr/sbin/httpd
  10. apache 0.0 0.2 ? S : : /usr/sbin/httpd
  11. apache 0.0 0.2 ? S : : /usr/sbin/httpd
  12. apache 0.0 0.2 ? S : : /usr/sbin/httpd
  13. root 0.0 0.0 ? S+ : : grep --color=auto httpd
  14.  
  15. /* !!!!但对于外部来说,是无法访问容器里的httpd的 */
  16.  
  17. //先利用容器生成镜像
  18. [root@localhost ~]# docker commit -m "centos_with_httpd" -a "frankie" 30c centos_with_httpd:frankie
  19. fb83cd744da57dba7fb3e5bf861bd0d014da7508b8f47adeb1a3fd4ac01252ed
  20. [root@localhost ~]# docker images
  21. REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
  22. centos_with_httpd frankie fb83cd744da5 minutes ago 325.8 MB
  23.  
  24. // -p 可以指定端口映射
  25. [root@localhost ~]# docker run -itd -p : centos_with_httpd:frankie bash
  26. 3f043c0dc5b456e53ff040d53d1455cbaa6bedad7d35954be3718a859bea8c24
  27.  
  28. //进入映射了端口的容器里
  29. [root@localhost ~]# docker exec -it 3f0 bash
  30.  
  31. //启动httpd服务
  32. [root@3f043c0dc5b4 /]# /usr/sbin/httpd
  33. AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.12. Set the 'ServerName' directive globally to suppress this message
  34. [root@3f043c0dc5b4 /]# ps aux|grep httpd
  35. root 0.1 0.3 ? Ss : : /usr/sbin/httpd
  36. apache 0.0 0.2 ? S : : /usr/sbin/httpd
  37. apache 0.0 0.2 ? S : : /usr/sbin/httpd
  38. apache 0.0 0.2 ? S : : /usr/sbin/httpd
  39. apache 0.0 0.2 ? S : : /usr/sbin/httpd
  40. apache 0.0 0.2 ? S : : /usr/sbin/httpd
  41. root 0.0 0.0 ? S+ : : grep --color=auto httpd
  42.  
  43. //成功启动httpd,所以可以连接到
  44. [root@3f043c0dc5b4 /]# curl localhost
  45. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head>
  46. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  47. <title>Apache HTTP Server Test Page powered by CentOS</title>
  48. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  49.  
  50. <!-- Bootstrap -->
  51. <link href="/noindex/css/bootstrap.min.css" rel="stylesheet">
  52. <link rel="stylesheet" href="noindex/css/open-sans.css" type="text/css" />
  53.  
  54. <style type="text/css"><!--
  55. ...
  56. ...
  57.  
  58. [root@3f043c0dc5b4 /]# vi /var/www/html/.html
  59. [root@3f043c0dc5b4 /]# curl localhost/.html
  60. frankielinux.com
  61. [root@3f043c0dc5b4 /]# exit
  62.  
  63. //回到宿主机 ,查看docker的ip
  64. [root@localhost ~]# ifconfig
  65. docker0 Link encap:Ethernet HWaddr :::4F::
  66. inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
  67. inet6 addr: fe80::e443:adff:fe6d:3b2/ Scope:Link
  68. UP BROADCAST RUNNING MULTICAST MTU: Metric:
  69. RX packets: errors: dropped: overruns: frame:
  70. TX packets: errors: dropped: overruns: carrier:
  71. collisions: txqueuelen:
  72. RX bytes: (469.6 KiB) TX bytes: (39.1 MiB)
  73.  
  74. //通过httpd连接,则可以在外部连接容器
  75. [root@localhost ~]# curl 172.17.42.1:/.html
  76. frankielinux.com
  77.  
  78. //这个容器有端口映射
  79. [root@localhost ~]# docker ps
  80. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  81. 3f043c0dc5b4 centos_with_httpd:frankie "bash" minutes ago Up minutes 0.0.0.0:->/tcp boring_ardinghelli
  1. /* 容器互联 */
  2. /* 所以可以开启一个新的容器,
  3.  
  4. 用Centos6的镜像来做一个容器--然后来用yum源来安装MySQL
  5. */
  6.  
  7. [root@localhost ~]# docker images
  8. REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
  9. centos_with_httpd frankie fb83cd744da5 About an hour ago 325.8 MB
  10. centos--x86 latest c37f3636c1f8 hours ago 343.8 MB
  11. centos_with_net latest c5b412fe1c33 hours ago 294.1 MB
  12. centos latest d83a55af4e75 weeks ago 196.7 MB
  13. frankie latest d83a55af4e75 weeks ago 196.7 MB
  14. registry latest ad8da6d14f6d weeks ago 33.28 MB
  15. [root@localhost ~]# docker run -itd centos--x86 bash
  16. faaa5d792a21f3735e4ade09a9767ab90a54c13b19084a9b004b4dd595615310
  17. [root@localhost ~]# docker exec -it faaa bash
  18. [root@faaa5d792a21 /]# yum install -y mysql-server
  19. Loaded plugins: fastestmirror
  20. Setting up Install Process
  21. base | 3.7 kB :
  22. base/primary_db | 4.7 MB :
  23. extras | 3.4 kB :
  24. extras/primary_db | kB :
  25. [root@faaa5d792a21 /]# /etc/init.d/mysqld start
  26. Initializing MySQL database: Installing MySQL system tables...
  27. OK
  28. Filling help tables...
  29. OK
  30.  
  31. [root@faaa5d792a21 /]# netstat -lnp
  32. Active Internet connections (only servers)
  33. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  34. tcp 0.0.0.0: 0.0.0.0:* LISTEN -
  35. Active UNIX domain sockets (only servers)
  36. Proto RefCnt Flags Type State I-Node PID/Program name Path
  37. unix [ ACC ] STREAM LISTENING - /var/lib/mysql/mysql.sock
  38. [root@faaa5d792a21 /]# exit
  39. [root@localhost ~]# docker commit -m "centos_with_mysql" -a "frankie" faaa centos6_with_mysql
  40. 5c15987b3c3ac435be66b773410384bd2b17e4ac640876ab0687a931ee1bb0fb
  41. [root@localhost ~]# docker run -itd -p : centos6_with_mysql bash
  42. afbe47d822beccbb74bd379974b9f2507ac56c2c71a176ab41aceaa7b269aed4
  43. [root@localhost ~]# docker ps
  44. CONTAINER ID IMAGE COMMAND CREATED PORTS NAMES
  45. afbe47d822be centos6_with_mysql "bash" seconds ag 0.0.0.0:->/tcp ecstatic_sinoussi
  46.  
  47. [root@localhost ~]# docker run -itd -p : --name web --link ecstatic_sinou ssi:db centos_with_httpd:frankie
  48. a21afaa4da5bcb8c6197bf781a6731cfaf28a853a06fe865225a9897f1eb743d
  49. [root@localhost ~]# docker ps
  50. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  51. a21afaa4da5b centos_with_httpd:frankie "bash" seconds a go Up seconds 0.0.0.0:->/tcp web
  52. [root@localhost ~]# docker exec -it web bash
  53. [root@a21afaa4da5b /]# ping db
  54. PING db (172.17.0.14) () bytes of data.
  55. bytes from db (172.17.0.14): icmp_seq= ttl= time=22.1 ms
  56. bytes from db (172.17.0.14): icmp_seq= ttl= time=0.065 ms
  57. ^C
  58. --- db ping statistics ---
  59. packets transmitted, received, % packet loss, time 1002ms
  60. rtt min/avg/max/mdev = 0.065/11.105/22.146/11.041 ms
  61. [root@a21afaa4da5b /]# cat /etc/hosts
  62. 172.17.0.15 a21afaa4da5b
  63. 127.0.0.1 localhost
  64. :: localhost ip6-localhost ip6-loopback
  65. fe00:: ip6-localnet
  66. ff00:: ip6-mcastprefix
  67. ff02:: ip6-allnodes
  68. ff02:: ip6-allrouters
  69. 172.17.0.14 db afbe47d822be ecstatic_sinoussi

docker的四种网络模式的更多相关文章

  1. Docker——四种网络模式

    docker run创建Docker容器时,可以用–net选项指定容器的网络模式,Docker有以下4种网络模式:  bridge模式:使用–net =bridge指定,默认设置:  host模式 ...

  2. [转帖]Docker四种网络模式

    Docker(十四)-Docker四种网络模式 https://www.cnblogs.com/zhuochong/p/10069293.html 计算机网络相关的知识 非常有用.. Docker 安 ...

  3. Docker学习第四天(Docker四种网络模式)

    Docker四种网络模式 实现原理 Docker使用Linux桥接(参考<Linux虚拟网络技术>),在宿主机虚拟一个Docker容器网桥(docker0),Docker启动一个容器时会根 ...

  4. Docker 四种网络模式

    原文 https://www.cnblogs.com/gispathfinder/p/5871043.html 我们在使用docker run创建Docker容器时,可以用--net选项指定容器的网络 ...

  5. Docker的4种网络模式

    我们在使用docker run创建Docker容器时,可以用--net选项指定容器的网络模式,Docker有以下4种网络模式: · host模式,使用--net=host指定. · container ...

  6. [docker]docker的四种网络方式

    声明: 本博客欢迎转发,但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! bridge方式(默认) H ...

  7. Docker的4种网络模式详细介绍

    docker run创建Docker容器时,可以用–net选项指定容器的网络模式,Docker有以下4种网络模式: bridge模式:使用–net =bridge指定: host模式:使用–net = ...

  8. Docker(十四)-Docker四种网络模式

    Docker 安装时会自动在 host 上创建三个网络,我们可用 docker network ls 命令查看: none模式,使用--net=none指定,该模式关闭了容器的网络功能. host模式 ...

  9. vbox的四种网络模式

      一.NAT模式 特点: 1.如果主机可以上网,虚拟机可以上网 2.虚拟机之间不能ping通 3.虚拟机可以ping通主机(此时ping虚拟机的网关,即是ping主机) 4.主机不能ping通虚拟机 ...

随机推荐

  1. Linux下巧用my.cnf,mysql连接服务器不需要输入账号密码信息

    Linux下每次用mysql连接连接服务器,常常用如下方式: [root@localhost ~]# mysql -hlocalhost -uroot -p11111 每次都输入用户名,密码,多折腾人 ...

  2. beta版本项目冲刺

    项目冲刺第一天 项目冲刺第二天 项目冲刺第三天 项目冲刺第四天 项目冲刺第五天 项目冲刺第六天 项目冲刺第七天

  3. 【团队项目演示】FZU5BOYS之团队项目链接汇总

    FZU5BOYS      项目冲刺之博客汇总 Alpha版本 Day One Day Two Day Three Day Four Day Five Day Six Day Seven Day Ei ...

  4. Linux_日志信息

    一.httpd日志:/var/log/httpd1.软件位置:whereis httpd2.配置文件位置:/etc/httpd/conf/httpd.conf 二.mysql日志:/var/log 查 ...

  5. 使用quartz 定时任务

    Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度. ...

  6. hive 使用笔记(partition; HDFS乱码)

    6.  insert 语句 1) 因为目标表有partition, 所以刚开始我使用的语句是 insert overwrite table sa_r_item_sales_day_week_month ...

  7. 【poj1088】 滑雪

    http://poj.org/problem?id=1088 (题目链接) 题意 给出一个矩阵,任意选择一个起点,每次只能向周围4个格子中的值比当前格子小的格子移动,求最多能移动多少步. Soluti ...

  8. SqlParameter中的size

    SqlParameter中size对于需要指定大小的数据库中的数据类型参数有影响[如nvarchar],如果对于这些类型没有指定size则会默认根据赋的值进行推导应该指定的size,而对于那些大小固定 ...

  9. Java JDBC下执行SQL的不同方式、参数化预编译防御

    相关学习资料 http://zh.wikipedia.org/wiki/Java数据库连接 http://lavasoft.blog.51cto.com/62575/20588 http://blog ...

  10. Tomcat 404

    原因:servlet没有配置正确 ,查看web.xml确认正确,以及自己的请求路径正确 在IE中提示"404"错误有以下三种情况 1.未部署Web应用 2.URL输入错误 排错方法 ...