## 1.容器之间通信原理。
![](https://img.kancloud.cn/68/ef/68ef41dadd7bbe02dfcfe73ca888b486_719x499.png)
>图中不是veth 而是eth.
> 所以两个容器之间是可以相互访问的。
> 容器也是可以访问外网的。
* 查看容器网络
```
[root@VM_0_11_centos ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
8b6bb29854ad bridge bridge local
ef67345c3edf host host local
fdec26c25201 none null local
```
> 容器是链接bridge网络的。
* 在宿主机运行ip a命令
```
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:46:7d:24:3c brd ff:ff:ff:ff:ff:ff
inet 172.18.0.1/16 brd 172.18.255.255 scope global docker0
valid_lft forever preferred_lft forever
19: veth269b564@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP
link/ether d6:cf:71:81:0c:f7 brd ff:ff:ff:ff:ff:ff link-netnsid 1
33: veth63f619c@if32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP
link/ether ce:31:7b:fb:f0:f9 brd ff:ff:ff:ff:ff:ff link-netnsid 0
37: veth8c78dc6@if36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP
link/ether fe:9b:91:60:65:a5 brd ff:ff:ff:ff:ff:ff link-netnsid 2
```
* 因为我启动了三个容器,所以有三个eth连在docker0上。
## 2.容器之间link
* 运行两个容器,进入查看ip
```
[root@VM_0_11_centos ~]# docker exec -it brave_pike /bin/sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
36: eth0@if37: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:12:00:04 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.4/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # ping www.baidu.com
PING www.baidu.com (180.101.49.11): 56 data bytes
64 bytes from 180.101.49.11: seq=0 ttl=47 time=7.548 ms
64 bytes from 180.101.49.11: seq=1 ttl=47 time=7.662 ms
```
```
[root@VM_0_11_centos ~]# docker run -itd busybox /bin/sh
a063a351dd9193c5845acd64ee65f4d26a3959ec8a605311a90269cfad0cf4d4
[root@VM_0_11_centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a063a351dd91 busybox "/bin/sh" 26 seconds ago Up 24 seconds amazing_dewdney
0543cb9b4df1 busybox "/bin/sh" 9 hours ago Up 9 hours brave_pike
09342d7e903d bizzbee/centos-in-vim "/bin/bash" 11 hours ago Up 11 hours focused_hertz
30bc495d1944 bizzbee/python-flask-python3.7 "python app.py" 2 days ago Up 2 days 0.0.0.0:5000->5000/tcp loving_euclid
[root@VM_0_11_centos ~]# docker exec -it amazing_dewdney /bin/sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
38: eth0@if39: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:12:00:05 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.5/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
```
> 他们互相是可以用ip ping通的。原因上面说了。
> 但是他们不能用名字ping通。如果要用名字ping通。启动时要加link参数。
* 删掉一个容器,重新启动一个,link到另一个容器。
```
[root@VM_0_11_centos ~]# docker run -itd --link brave_pike busybox /bin/sh
b30821e691f5398971524880abc3e334fd2c6e1bdabbf3096aa932f758679af4
[root@VM_0_11_centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b30821e691f5 busybox "/bin/sh" 2 seconds ago Up 1 second blissful_cori
0543cb9b4df1 busybox "/bin/sh" 9 hours ago Up 9 hours brave_pike
09342d7e903d bizzbee/centos-in-vim "/bin/bash" 11 hours ago Up 11 hours focused_hertz
30bc495d1944 bizzbee/python-flask-python3.7 "python app.py" 2 days ago Up 2 days 0.0.0.0:5000->5000/tcp loving_euclid
```
* 创建成功后进入新创建的容器。
```
[root@VM_0_11_centos ~]# docker exec -it blissful_cori /bin/sh
ping brave_pike
PING brave_pike (172.18.0.4): 56 data bytes
64 bytes from 172.18.0.4: seq=0 ttl=64 time=0.106 ms
64 bytes from 172.18.0.4: seq=1 ttl=64 time=0.069 ms
64 bytes from 172.18.0.4: seq=2 ttl=64 time=0.076 ms
```
* 成功了。
## 3.容器端口映射
> 只需要启动容器的时候加上-p参数,映射到宿主机的端口。
```
docker run -p 5000:5000 bizzbee/python-flask-python3.7
```
* 以上把容器的5000端口到宿主机5000端口。