首页 > 代码库 > docker的四种网络模式
docker的四种网络模式
/* 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/httpdAH00558: 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 httpdroot 111 0.1 0.3 221856 3480 ? Ss 11:41 0:00 /usr/sbin/httpdapache 112 0.0 0.2 221856 2632 ? S 11:41 0:00 /usr/sbin/httpdapache 113 0.0 0.2 221856 2632 ? S 11:41 0:00 /usr/sbin/httpdapache 114 0.0 0.2 221856 2632 ? S 11:41 0:00 /usr/sbin/httpdapache 115 0.0 0.2 221856 2632 ? S 11:41 0:00 /usr/sbin/httpdapache 116 0.0 0.2 221856 2632 ? S 11:41 0:00 /usr/sbin/httpdroot 118 0.0 0.0 8988 812 ? S+ 11:41 0:00 grep --color=auto httpd/* !!!!但对于外部来说,是无法访问容器里的httpd的 *///先利用容器生成镜像[root@localhost ~]# docker commit -m "centos_with_httpd" -a "frankie" 30c centos_with_httpd:frankiefb83cd744da57dba7fb3e5bf861bd0d014da7508b8f47adeb1a3fd4ac01252ed[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEcentos_with_httpd frankie fb83cd744da5 2 minutes ago 325.8 MB// -p 可以指定端口映射[root@localhost ~]# docker run -itd -p 5123:80 centos_with_httpd:frankie bash3f043c0dc5b456e53ff040d53d1455cbaa6bedad7d35954be3718a859bea8c24//进入映射了端口的容器里[root@localhost ~]# docker exec -it 3f0 bash//启动httpd服务[root@3f043c0dc5b4 /]# /usr/sbin/httpdAH00558: 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 httpdroot 33 0.1 0.3 221856 3508 ? Ss 11:55 0:00 /usr/sbin/httpdapache 34 0.0 0.2 221856 2632 ? S 11:55 0:00 /usr/sbin/httpdapache 35 0.0 0.2 221856 2632 ? S 11:55 0:00 /usr/sbin/httpdapache 36 0.0 0.2 221856 2632 ? S 11:55 0:00 /usr/sbin/httpdapache 37 0.0 0.2 221856 2632 ? S 11:55 0:00 /usr/sbin/httpdapache 38 0.0 0.2 221856 2632 ? S 11:55 0:00 /usr/sbin/httpdroot 40 0.0 0.0 8988 812 ? S+ 11:55 0:00 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=http://www.mamicode.com/"/noindex/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href=http://www.mamicode.com/"noindex/css/open-sans.css" type="text/css" /><style type="text/css"><!--......[root@3f043c0dc5b4 /]# vi /var/www/html/1.html[root@3f043c0dc5b4 /]# curl localhost/1.htmlfrankielinux.com[root@3f043c0dc5b4 /]# exit//回到宿主机 ,查看docker的ip[root@localhost ~]# ifconfigdocker0 Link encap:Ethernet HWaddr 12:90:97:4F:79:75 inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::e443:adff:fe6d:3b2/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:11858 errors:0 dropped:0 overruns:0 frame:0 TX packets:30521 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:480913 (469.6 KiB) TX bytes:41003387 (39.1 MiB)//通过httpd连接,则可以在外部连接容器[root@localhost ~]# curl 172.17.42.1:5123/1.htmlfrankielinux.com//这个容器有端口映射[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3f043c0dc5b4 centos_with_httpd:frankie "bash" 10 minutes ago Up 10 minutes 0.0.0.0:5123->80/tcp boring_ardinghelli
/* 容器互联 *//* 所以可以开启一个新的容器, 用Centos6的镜像来做一个容器--然后来用yum源来安装MySQL*/[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEcentos_with_httpd frankie fb83cd744da5 About an hour ago 325.8 MBcentos-6-x86 latest c37f3636c1f8 21 hours ago 343.8 MBcentos_with_net latest c5b412fe1c33 22 hours ago 294.1 MBcentos latest d83a55af4e75 4 weeks ago 196.7 MBfrankie latest d83a55af4e75 4 weeks ago 196.7 MBregistry latest ad8da6d14f6d 4 weeks ago 33.28 MB[root@localhost ~]# docker run -itd centos-6-x86 bashfaaa5d792a21f3735e4ade09a9767ab90a54c13b19084a9b004b4dd595615310[root@localhost ~]# docker exec -it faaa bash[root@faaa5d792a21 /]# yum install -y mysql-serverLoaded plugins: fastestmirrorSetting up Install Processbase | 3.7 kB 00:00base/primary_db | 4.7 MB 00:06extras | 3.4 kB 00:00extras/primary_db | 37 kB 00:00[root@faaa5d792a21 /]# /etc/init.d/mysqld startInitializing MySQL database: Installing MySQL system tables...OKFilling help tables...OK[root@faaa5d792a21 /]# netstat -lnpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN - Active UNIX domain sockets (only servers)Proto RefCnt Flags Type State I-Node PID/Program name Pathunix 2 [ ACC ] STREAM LISTENING 27434 - /var/lib/mysql/mysql.sock[root@faaa5d792a21 /]# exit[root@localhost ~]# docker commit -m "centos_with_mysql" -a "frankie" faaa centos6_with_mysql5c15987b3c3ac435be66b773410384bd2b17e4ac640876ab0687a931ee1bb0fb[root@localhost ~]# docker run -itd -p 13306:3306 centos6_with_mysql bashafbe47d822beccbb74bd379974b9f2507ac56c2c71a176ab41aceaa7b269aed4[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED PORTS NAMESafbe47d822be centos6_with_mysql "bash" 5 seconds ag 0.0.0.0:13306->3306/tcp ecstatic_sinoussi[root@localhost ~]# docker run -itd -p 18080:80 --name web --link ecstatic_sinou ssi:db centos_with_httpd:frankiea21afaa4da5bcb8c6197bf781a6731cfaf28a853a06fe865225a9897f1eb743d[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa21afaa4da5b centos_with_httpd:frankie "bash" 15 seconds a go Up 9 seconds 0.0.0.0:18080->80/tcp web[root@localhost ~]# docker exec -it web bash[root@a21afaa4da5b /]# ping dbPING db (172.17.0.14) 56(84) bytes of data.64 bytes from db (172.17.0.14): icmp_seq=1 ttl=64 time=22.1 ms64 bytes from db (172.17.0.14): icmp_seq=2 ttl=64 time=0.065 ms^C--- db ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 1002msrtt min/avg/max/mdev = 0.065/11.105/22.146/11.041 ms[root@a21afaa4da5b /]# cat /etc/hosts172.17.0.15 a21afaa4da5b127.0.0.1 localhost::1 localhost ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allrouters172.17.0.14 db afbe47d822be ecstatic_sinoussi
docker的四种网络模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。