/*

    1. host模式 :

                        docker run 使用 --net=host指定

        docker使用的网络实际上和宿主机一样

    2. container模式:

                        使用 --net=container:container_id/container_name

        多个容器使用共同的网络,看到的ip是一样的。

    3. none 模式

                        使用 --net=none指定

        这种模式下,不会配置任何网络。

     4. bridge模式

                        使用 --net=bridge指定

        默认模式,不会指定

                此模式会为每个容器分配一个独立的network namespace
*/
/* 外部网络访问容器 :外部的用户要访问容器,先将容器的ip映射出去,然后客户利用宿主机的ip来访问*/
[root@30c1fec5df6a /]# yum install -y httpd
//虽然报错,但是httpd已经启动
[root@30c1fec5df6a /]# /usr/sbin/httpd
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
[root@30c1fec5df6a /]# ps aux|grep httpd
root 0.1 0.3 ? Ss : : /usr/sbin/httpd
apache 0.0 0.2 ? S : : /usr/sbin/httpd
apache 0.0 0.2 ? S : : /usr/sbin/httpd
apache 0.0 0.2 ? S : : /usr/sbin/httpd
apache 0.0 0.2 ? S : : /usr/sbin/httpd
apache 0.0 0.2 ? S : : /usr/sbin/httpd
root 0.0 0.0 ? S+ : : grep --color=auto httpd /* !!!!但对于外部来说,是无法访问容器里的httpd的 */ //先利用容器生成镜像
[root@localhost ~]# docker commit -m "centos_with_httpd" -a "frankie" 30c centos_with_httpd:frankie
fb83cd744da57dba7fb3e5bf861bd0d014da7508b8f47adeb1a3fd4ac01252ed
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos_with_httpd frankie fb83cd744da5 minutes ago 325.8 MB // -p 可以指定端口映射
[root@localhost ~]# docker run -itd -p : centos_with_httpd:frankie bash
3f043c0dc5b456e53ff040d53d1455cbaa6bedad7d35954be3718a859bea8c24 //进入映射了端口的容器里
[root@localhost ~]# docker exec -it 3f0 bash //启动httpd服务
[root@3f043c0dc5b4 /]# /usr/sbin/httpd
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
[root@3f043c0dc5b4 /]# ps aux|grep httpd
root 0.1 0.3 ? Ss : : /usr/sbin/httpd
apache 0.0 0.2 ? S : : /usr/sbin/httpd
apache 0.0 0.2 ? S : : /usr/sbin/httpd
apache 0.0 0.2 ? S : : /usr/sbin/httpd
apache 0.0 0.2 ? S : : /usr/sbin/httpd
apache 0.0 0.2 ? S : : /usr/sbin/httpd
root 0.0 0.0 ? S+ : : grep --color=auto httpd //成功启动httpd,所以可以连接到
[root@3f043c0dc5b4 /]# curl localhost
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Apache HTTP Server Test Page powered by CentOS</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- Bootstrap -->
<link href="/noindex/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="noindex/css/open-sans.css" type="text/css" /> <style type="text/css"><!--
...
... [root@3f043c0dc5b4 /]# vi /var/www/html/.html
[root@3f043c0dc5b4 /]# curl localhost/.html
frankielinux.com
[root@3f043c0dc5b4 /]# exit //回到宿主机 ,查看docker的ip
[root@localhost ~]# ifconfig
docker0 Link encap:Ethernet HWaddr :::4F::
inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::e443:adff:fe6d:3b2/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (469.6 KiB) TX bytes: (39.1 MiB) //通过httpd连接,则可以在外部连接容器
[root@localhost ~]# curl 172.17.42.1:/.html
frankielinux.com //这个容器有端口映射
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f043c0dc5b4 centos_with_httpd:frankie "bash" minutes ago Up minutes 0.0.0.0:->/tcp boring_ardinghelli
/* 容器互联 */
/* 所以可以开启一个新的容器, 用Centos6的镜像来做一个容器--然后来用yum源来安装MySQL
*/ [root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos_with_httpd frankie fb83cd744da5 About an hour ago 325.8 MB
centos--x86 latest c37f3636c1f8 hours ago 343.8 MB
centos_with_net latest c5b412fe1c33 hours ago 294.1 MB
centos latest d83a55af4e75 weeks ago 196.7 MB
frankie latest d83a55af4e75 weeks ago 196.7 MB
registry latest ad8da6d14f6d weeks ago 33.28 MB
[root@localhost ~]# docker run -itd centos--x86 bash
faaa5d792a21f3735e4ade09a9767ab90a54c13b19084a9b004b4dd595615310
[root@localhost ~]# docker exec -it faaa bash
[root@faaa5d792a21 /]# yum install -y mysql-server
Loaded plugins: fastestmirror
Setting up Install Process
base | 3.7 kB :
base/primary_db | 4.7 MB :
extras | 3.4 kB :
extras/primary_db | kB :
[root@faaa5d792a21 /]# /etc/init.d/mysqld start
Initializing MySQL database: Installing MySQL system tables...
OK
Filling help tables...
OK [root@faaa5d792a21 /]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0.0.0.0: 0.0.0.0:* LISTEN -
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix [ ACC ] STREAM LISTENING - /var/lib/mysql/mysql.sock
[root@faaa5d792a21 /]# exit
[root@localhost ~]# docker commit -m "centos_with_mysql" -a "frankie" faaa centos6_with_mysql
5c15987b3c3ac435be66b773410384bd2b17e4ac640876ab0687a931ee1bb0fb
[root@localhost ~]# docker run -itd -p : centos6_with_mysql bash
afbe47d822beccbb74bd379974b9f2507ac56c2c71a176ab41aceaa7b269aed4
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED PORTS NAMES
afbe47d822be centos6_with_mysql "bash" seconds ag 0.0.0.0:->/tcp ecstatic_sinoussi [root@localhost ~]# docker run -itd -p : --name web --link ecstatic_sinou ssi:db centos_with_httpd:frankie
a21afaa4da5bcb8c6197bf781a6731cfaf28a853a06fe865225a9897f1eb743d
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a21afaa4da5b centos_with_httpd:frankie "bash" seconds a go Up seconds 0.0.0.0:->/tcp web
[root@localhost ~]# docker exec -it web bash
[root@a21afaa4da5b /]# ping db
PING db (172.17.0.14) () bytes of data.
bytes from db (172.17.0.14): icmp_seq= ttl= time=22.1 ms
bytes from db (172.17.0.14): icmp_seq= ttl= time=0.065 ms
^C
--- db ping statistics ---
packets transmitted, received, % packet loss, time 1002ms
rtt min/avg/max/mdev = 0.065/11.105/22.146/11.041 ms
[root@a21afaa4da5b /]# cat /etc/hosts
172.17.0.15 a21afaa4da5b
127.0.0.1 localhost
:: localhost ip6-localhost ip6-loopback
fe00:: ip6-localnet
ff00:: ip6-mcastprefix
ff02:: ip6-allnodes
ff02:: ip6-allrouters
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. python 中颜色的表示

    字背景颜色范围:40----49 40:黑 41:深红 42:绿 43:黄色 44:蓝色 45:紫色 46:深绿 47:白色 字颜色:30-----------39 30:黑 31:红 32:绿 33 ...

  2. PHP函数可变参数列表的具体实现方法介绍

    PHP函数可变参数列表可以通过_get_args().func_num_args().func_get_arg()这三个函数来实现.我们下面就对此做了详细的介绍. AD:2014WOT全球软件技术峰会 ...

  3. Day5_作业

     ATM作业:1.额度15000或自定义2.实现购物商城,买东西加入购物车,调用信用卡接口结账3.可以提现,手续费5%4.每月22号出账单,每月10号为还款日,过期未还,按欠款总额万分之5每日计息5. ...

  4. myeclipse 配置weblogic

    1.打开myeclipse,选择Window -> Preferences--->MyEclipse--->servers 2.点击servers---->weblogic-- ...

  5. BZOJ-4010 菜肴制作 贪心+堆+(拓扑图拓扑序)

    无意做到...char哥还中途强势插入干我...然后据他所言,看了一会题,一转头,我爆了正解....可怕 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory L ...

  6. MyEclipse------PreparedStatement使用方法

    testPreparedStatement.jsp <%@ page language="java" import="java.util.*" pageE ...

  7. PHP利用Filesystem函数实现操作缓存(生成,获取,删除操作)

    <?php class File{ //$key 相当于缓存文件的文件名 //$value 缓存数据 private $_dir;//定义默认路径 const EXT='.txt'; publi ...

  8. ci 4.2

    超级对象 $this 当前的控制器对象 有很多属性 $this->load    装载器类的实例在systme/core/loader.php  里面有view方法 $obj = new CI_ ...

  9. Protocol Buffer技术详解(数据编码)

    Protocol Buffer技术详解(数据编码) 之前已经发了三篇有关Protocol Buffer的技术博客,其中第一篇介绍了Protocol Buffer的语言规范,而后两篇则分别基于C++和J ...

  10. linux进程调度之 FIFO 和 RR 调度策略

    转载 http://blog.chinaunix.net/uid-24774106-id-3379478.html    linux进程调度之 FIFO 和 RR 调度策略 2012-10-19 18 ...