docker网络通讯及容器卷通信卷!

Posted 龙少。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker网络通讯及容器卷通信卷!相关的知识,希望对你有一定的参考价值。

docker网络通讯及容器卷通信卷

一.docker网络模式

1.docker容器的虚拟网关

(1)loopback

回环网卡、TCP/IP网卡是否生效

(2)virtual bridge

linux自身继承了一个虚拟化功能(kvm架构),是原生架构的一个虚拟化平台,安装了一个虚拟化平台之后就会系统就会自动安装虚拟网卡
(示例:安装workstation ( 虚拟化平台)之后,会在网络适配器中会多出VMnet1、VMnet8、VMnet0)

(3)docker 0

容器的网关,绑定物理网卡,负责做NAT地址转换、端口映射;docker0本身也是一种容器

(4)veth对

一组虚拟设备,用户连接两个不同的名称空间;宿主机内就是veth


2.docker的4种网络模式

(1)Host模式

net=host;容器和宿主机共享Network namespace(网络名称空间/网络协议栈)

(2)Container模式

net=container:NAME or ID;多个容器共享一 个Network namespace 。

(3)None模式

net=none;容器有独立的Network namespace,但并没有对其进行任何网络设置,如分配veth pair和网桥连接,配置IP等(自闭空间)

(4)Bridge模式

net=bridge(默认为该模式);默认模式通过Veth对连接容器与docker0网桥,网桥分配给容器IP,同时docker 0作为“局域网”内容器的网关,最后和宿主机网卡进行通讯

以上不需要动手配置,真正需要配置的是自定义网络

3.docker自定义网络

(1)查看网络列表

[root@docker ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
9477282009db   bridge    bridge    local
7f91145d9494   host      host      local
24131737de22   none      null      local

(2)查看容器

docker inspect 镜像id 查看镜像状态

[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
tomcat       test      d6598c118ba0   2 days ago      599MB
nginx        new       8cc8975756f1   2 days ago      681MB
nginx        latest    822b7ec2aaf2   8 days ago      133MB
centos       7         8652b9f0cb4c   10 months ago   204MB

[root@docker ~]# docker run -itd --name centos centos:7 /bin/bash
378fe5ac181efae8fc5e0dfad3729f834f4c41b89e82f12afb4fb97c3d1e92af

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS                                         NAMES
378fe5ac181e   centos:7      "/bin/bash"              16 seconds ago   Up 15 seconds                                                 centos
e2fe9ef4b577   tomcat:test   "/usr/local/src/tomc…"   2 days ago       Up 2 days       0.0.0.0:49154->8080/tcp, :::49154->8080/tcp   cool_hermann
9639ed027de8   nginx:new     "/bin/sh -c nginx"       2 days ago       Up 2 days       0.0.0.0:49153->80/tcp, :::49153->80/tcp       wizardly_maxwell

[root@docker ~]# docker exec centos ps aux					#使用exec执行命令
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.1  0.0  11828  1656 pts/0    Ss+  17:32   0:00 /bin/bash
root         16  0.0  0.0  51732  1704 ?        Rs   17:33   0:00 ps aux
您在 /var/spool/mail/root 中有邮件
[root@docker ~]# docker inspect centos						#使用指定的名称查看容器


(3)自定义网络固定ip

–network:指定网络类型
–ip:指定ip地址

[root@docker ~]# docker run -itd --name test1 --network bridge --ip 172.17.0.10 centos:latest /bin/bash
97ea02cf3fad266e746969fbb502949bf705c0e1b040675d78e46e7ccfe95bdc
docker: Error response from daemon: user specified IP address is supported on user defined networks only.

这种方式会报错:只能遵守默认的分配地址方式;无法指定

① 先自定义网络模式

[root@docker ~]# docker network create --subnet=172.18.0.0/16 mynetwork
8ec784189c9a83bb6eb4ab6bc01128e86e297f5867659b96cd1f7d03f175d936
您在 /var/spool/mail/root 中有邮件
[root@docker ~]# docker network ls
NETWORK ID     NAME        DRIVER    SCOPE
9477282009db   bridge      bridge    local
7f91145d9494   host        host      local
8ec784189c9a   mynetwork   bridge    local
24131737de22   none        null      local

② 加入指定ip

[root@docker ~]# docker run -itd --name centos-2 --net mynetwork --ip 172.18.0.10 centos:latest /bin/bash
70daaad53f1c589972ff6bf2c583c4345dc984dbf0e0b0efcda74cd1562d3aae
[root@docker ~]# docker inspect centos-2

(4)暴露端口

查询虽然有80端口但是无法打开网页

① -p 指定端口

[root@docker ~]# docker run -itd -p 444:80 nginx /bin/bash
cf5d03c5220cc04e4b5da89d55ec9eabcc63ad47536c778112ad5fa1ec2616bf
您在 /var/spool/mail/root 中有邮件
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                         NAMES
cf5d03c5220c   nginx           "/docker-entrypoint.…"   14 seconds ago   Up 13 seconds   0.0.0.0:444->80/tcp, :::444->80/tcp           loving_dijkstra
6245e722475b   nginx:latest    "/docker-entrypoint.…"   2 minutes ago    Up 2 minutes    80/tcp                                        friendly_morse
70daaad53f1c   centos:latest   "/bin/bash"              7 minutes ago    Up 7 minutes                                                  centos-2
97ea02cf3fad   centos:latest   "/bin/bash"              13 minutes ago   Created                                                       test1
27dba7291477   centos:latest   "/bin/bash"              13 minutes ago   Created                                                       centos-3
378fe5ac181e   centos:7        "/bin/bash"              38 minutes ago   Up 38 minutes                                                 centos
e2fe9ef4b577   tomcat:test     "/usr/local/src/tomc…"   2 days ago       Up 2 days       0.0.0.0:49154->8080/tcp, :::49154->8080/tcp   cool_hermann
9639ed027de8   nginx:new       "/bin/sh -c nginx"       2 days ago       Up 2 days       0.0.0.0:49153->80/tcp, :::49153->80/tcp       wizardly_maxwell
[root@docker ~]# 
[root@docker ~]# docker run nginx:latest /bin/bash
您在 /var/spool/mail/root 中有邮件
[root@docker ~]# docker exec cf5d03c5220c nginx
2021/09/11 18:13:45 [notice] 7#7: using the "epoll" event method
2021/09/11 18:13:45 [notice] 7#7: nginx/1.21.1
2021/09/11 18:13:45 [notice] 7#7: built by gcc 8.3.0 (Debian 8.3.0-6) 
2021/09/11 18:13:45 [notice] 7#7: OS: Linux 3.10.0-957.el7.x86_64
2021/09/11 18:13:45 [notice] 7#7: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/09/11 18:13:45 [notice] 13#13: start worker processes
2021/09/11 18:13:45 [notice] 13#13: start worker process 14
2021/09/11 18:13:45 [notice] 13#13: start worker process 15
2021/09/11 18:13:45 [notice] 13#13: start worker process 16
2021/09/11 18:13:45 [notice] 13#13: start worker process 17


运行并启动nginx

查看网:192.168.206.188:444

② -P 随机端口

[root@docker ~]# docker run -itd -P nginx /bin/bash
00f5572ea156c091d86670aa395cd2dac790d8e51fcfa70d2353bb15e20aa2c9
您在 /var/spool/mail/root 中有邮件
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                     PORTS                                         NAMES
00f5572ea156   nginx           "/docker-entrypoint.…"   7 seconds ago    Up 6 seconds               0.0.0.0:49155->80/tcp, :::49155->80/tcp       goofy_agnesi
956491cfd9fd   nginx:latest    "/docker-entrypoint.…"   4 minutes ago    Exited (0) 4 minutes ago                                                 condescending_panini
cf5d03c5220c   nginx           "/docker-entrypoint.…"   6 minutes ago    Up 6 minutes               0.0.0.0:444->80/tcp, :::444->80/tcp           loving_dijkstra
6245e722475b   nginx:latest    "/docker-entrypoint.…"   8 minutes ago    Up 8 minutes               80/tcp                                        friendly_morse
70daaad53f1c   centos:latest   "/bin/bash"              13 minutes ago   Up 13 minutes                                                            centos-2
97ea02cf3fad   centos:latest   "/bin/bash"              19 minutes ago   Created                                                                  test1
27dba7291477   centos:latest   "/bin/bash"              20 minutes ago   Created                                                                  centos-3
378fe5ac181e   centos:7        "/bin/bash"              44 minutes ago   Up 44 minutes                                                            centos
e2fe9ef4b577   tomcat:test     "/usr/local/src/tomc…"   2 days ago       Up 2 days                  0.0.0.0:49154->8080/tcp, :::49154->8080/tcp   cool_hermann
9639ed027de8   nginx:new       "/bin/sh -c nginx"       2 days ago       Up 2 days                  0.0.0.0:49153->80/tcp, :::49153->80/tcp       wizardly_maxwell
[root@docker ~]# docker exec 00f5572ea156 nginx
2021/09/11 18:17:24 [notice] 7#7: using the "epoll" event method
2021/09/11 18:17:24 [notice] 7#7: nginx/1.21.1
2021/09/11 18:17:24 [notice] 7#7: built by gcc 8.3.0 (Debian 8.3.0-6) 
2021/09/11 18:17:24 [notice] 7#7: OS: Linux 3.10.0-957.el7.x86_64
2021/09/11 18:17:24 [notice] 7#7: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/09/11 18:17:24 [notice] 13#13: start worker processes
2021/09/11 18:17:24 [notice] 13#13: start worker process 14
2021/09/11 18:17:24 [notice] 13#13: start worker process 15
2021/09/11 18:17:24 [notice] 13#13: start worker process 16
2021/09/11 18:17:24 [notice] 13#13: start worker process 17
[root@docker ~]# 


查看网页192.168.206.188:49155

(5)在宿主机环境运行容器命令

docker exec -it 容器ID /bin/bash -c ‘nginx’
docker exec 容器ID/容器 name 执行的命令

二.docker数据卷

1.数据卷

数据卷是一个提供容器使用的特殊目录

创建数据卷

docker run -d -v /data1 -v /data2 --name web httpd:centos

挂载主机目录作为数据卷

docker run -d -v /var/www:/data1 --name web-1 httpd:centos

实例查看验证

[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
tomcat       test      d6598c118ba0   2 days ago      599MB
nginx        new       8cc8975756f1   2 days ago      681MB
nginx        latest    822b7ec2aaf2   8 days ago      133MB
centos       latest    300e315adb2f   9 months ago    209MB
centos       7         8652b9f0cb4c   10 months ago   204MB
您在 /var/spool/mail/root 中有邮件
[root@docker ~]# docker run -v /var/www:/data1 -v /var/html:/data2 -it --name centos-v4 centos:7 /bin/bash
[root@7d431361b5ef /]# cd /data1
[root@7d431361b5ef data1]# ls
[root@7d431361b5ef data1]# touch 1.txt
[root@7d431361b5ef data1]# cd /data2
[root@7d431361b5ef data2]# ls
[root@7d431361b5ef data2]# touch 2.txt
[root@7d431361b5ef data2]# ls /var/www
ls: cannot access /var/www: No such file or directory
[root@7d431361b5ef data2]# exit
exit
您在 /var/spool/mail/root 中有邮件
[root@docker ~]# ls /var/www
1.txt
[root@docker ~]# ls /var/html
2.txt

2.数据卷容器

就是一个普通的容器,实现容器间的互联互通

实例

[root@docker ~]# docker run --name web11 -v /data1 -v /data2 -it centos /bin/bash			#创建data1卷、data2卷
[root@75f345222ef9 /]# ls
bin  data1  data2  dev	etc  home  lib	lib64  lost+found  media  mnt  opt  proc  root	run  sbin  srv	sys  tmp  usr  var
[root@75f345222ef9 /]# cd data1 
[root@75f345222ef9 data1]# ls
[root@75f345222ef9 data1]# touch 1.txt
[root@75f345222ef9 data1]# ls
1.txt
[root@75f345222ef9 data1]# exit                                                          
exit


[root@docker ~]# docker run -it --volumes-from web11 --name tt centos /bin/bash				#允许一个容器,指定卷来源于web11,新的容器名字tt
[root@64816843721d /]# ls
bin  data1  data2  dev	etc  home  lib	lib64  lost+found  media  mnt  opt  proc  root	run  sbin  srv	sys  tmp  usr  var
[root@64816843721d /]# ls data1
1.txt
[root@64816843721d /]# 

3.容器互联

docker run -itd -P --name webb centos /bin/bash #创建并运行容器取名web1,端口号自动映射
docker run -itd -P --name web2 --link webb:webb centos /bin/bash #创建并运行容器取名web2,链接到web1和其通信
–link:打通隧道

[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
tomcat       test      d6598c118ba0   2 days ago      599MB
nginx        new       8cc8975756f1   2 days ago      681MB
nginx        latest    822b7ec2aaf2   8 days ago      133MB
centos       latest    300e315adb2f   9 months ago    209MB
centos       7         8652b9f0cb4c   10 months ago   204MB
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@docker ~]# docker ps -aq
[root@docker ~]# docker run -itd -P --name web888 centos /bin/bash
972416b46adeb73ac2321fe5c5997df431802ef7c34db3cdb18b58e091219da7
您在 /var/spool/mail/root 中有邮件
[root@docker ~]# docker run -itd -P --name web999 --link web888:web888 centos /bin/bash
4195d537f5aebe6a1f17b2bf5c7407a4d8b95722a2558c03511941c93de60bdf
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
4195d537f5ae   centos    "/bin/bash"   56 seconds ago       Up 54 seconds                 web999
972416b46ade   centos    "/bin/bash"   About a minute ago   Up About a minute             web888

[root@docker ~]# docker exec -it web888 /bin/bash
[root@972416b46ade /]# yum install -y net-tools

[root@972416b46ade /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 4385  bytes 16662126 (15.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4255  bytes 234697 (229.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,以上是关于docker网络通讯及容器卷通信卷!的主要内容,如果未能解决你的问题,请参考以下文章

docker网络通讯及容器卷通信卷!

Docker的数据管理及网络通信问题

Docker数据管理及网络通信

Docker的数据管理及网络通信

docker数据卷管理及网络基础配置

Docker的网络通讯,容器卷,通信卷~~~玩转它!!