作者主页(文火冰糖的硅基工坊):文火冰糖(王文兵)的博客_文火冰糖的硅基工坊_CSDN博客
本文网址:https://blog.csdn.net/HiWangWenBing/article/details/122724044
目录
前言:
第1章 主机TCP/IP端口与微服务端口之间进行映射
1.1 主机端口与docker端口映射示意图
1.2 端口映射参数
第2章 搭建容器之间的bridge-net,连接容器
docker最初与微服务绑定在一起的,在每个docker中,提供的一个微服务应用程序。无数个docker微服务组成一个网络,组成一个系统,每个微服务与微服务之间通过HTTP协议进行通信。本文就是探讨,如何构建docker微服务网络。
Docker 容器连接 | 菜鸟教程
(1)情形1
runoob@runoob:~$ docker run -d -P training/webapp python app.py
fce072cc88cee71b1cdceb57c2821d054a4a59f67da6b416fceb5593f059fc6d
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper
-P: 没有指定host主机的IP地址、端口和类型。
(2)情形2
我们也可以使用 -p 标识来指定容器端口绑定到主机端口。
两种方式的区别是:
指定端口:
runoob@runoob:~$ docker run -d -p 5000:5000 training/webapp python app.py
33e4523d30aaf0258915c368e66e03b49535de0ef20317d3f639d40222ba6bc0
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
33e4523d30aa training/webapp "python app.py" ... 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper
指定主机接口:
runoob@runoob:~$ docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py
95c6ceef88ca3e71eaf303c2833fd6701d8d1b2572b5613b5a932dfdfe8a857c
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
95c6ceef88ca training/webapp "python app.py" ... 5000/tcp, 127.0.0.1:5001->5000/tcp adoring_stonebraker
33e4523d30aa training/webapp "python app.py" ... 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper
指定端口类型:
runoob@runoob:~$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
6779686f06f6204579c1d655dd8b2b31e8e809b245a97b2d3a8e35abe9dcd22a
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
6779686f06f6 training/webapp "python app.py" ... 5000/tcp, 127.0.0.1:5000->5000/udp drunk_visvesvaraya
95c6ceef88ca training/webapp "python app.py" ... 5000/tcp, 127.0.0.1:5001->5000/tcp adoring_stonebraker
33e4523d30aa training/webapp "python app.py" ... 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper
(0)docker ps
[root@i-kfhwt9y5 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bff593077299 ubuntu "/bin/bash" 41 minutes ago Up 41 minutes test2
717d262a6b3e ubuntu "/bin/bash" 41 minutes ago Up 41 minutes test1
(1)建立docker bridge-net
$ docker network create -d bridge test-net
-d:参数指定 Docker 网络类型,有 bridge、overlay。
(2)主机端安装ping和ifconfig工具
yum install net-tools.x86_64 -y
(3)进入docker内部,安装ping和ifconfig工具
[root@i-kfhwt9y5 ~]# docker exec -it test1 /bin/bash
root@717d262a6b3e:/# apt-get update
root@717d262a6b3e:/# apt install iputils-ping
[root@i-kfhwt9y5 ~]# docker exec -it test2 /bin/bash
root@717d262a6b3e:/# apt-get update
root@717d262a6b3e:/# apt install iputils-ping
(4)显示test1和test2两个docker内部的IP地址
root@bff593077299:/# ifconfig
eth0: flags=4163 mtu 1500
inet 172.18.0.3 netmask 255.255.0.0 broadcast 172.18.255.255
ether 02:42:ac:12:00:03 txqueuelen 0 (Ethernet)
RX packets 12570 bytes 21863616 (21.8 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7537 bytes 709199 (709.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@717d262a6b3e:/# ifconfig
eth0: flags=4163 mtu 1500
inet 172.18.0.2 netmask 255.255.0.0 broadcast 172.18.255.255
ether 02:42:ac:12:00:02 txqueuelen 0 (Ethernet)
RX packets 11892 bytes 21820204 (21.8 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6416 bytes 603425 (603.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
(5)容器之间相互ping
root@717d262a6b3e:/# ping test1
PING test1 (172.18.0.2) 56(84) bytes of data.
64 bytes from 717d262a6b3e (172.18.0.2): icmp_seq=1 ttl=64 time=0.012 ms
64 bytes from 717d262a6b3e (172.18.0.2): icmp_seq=2 ttl=64 time=0.023 ms
64 bytes from 717d262a6b3e (172.18.0.2): icmp_seq=3 ttl=64 time=0.035 ms
root@717d262a6b3e:/# ping test2
PING test1 (172.18.0.2) 56(84) bytes of data.
64 bytes from 717d262a6b3e (172.18.0.3): icmp_seq=1 ttl=64 time=0.012 ms
64 bytes from 717d262a6b3e (172.18.0.3): icmp_seq=2 ttl=64 time=0.023 ms
64 bytes from 717d262a6b3e (172.18.0.3): icmp_seq=3 ttl=64 time=0.035 ms
至此,容器1和容器2之间通过bridge-net之间私网IP地址进行相互通信。
作者主页(文火冰糖的硅基工坊):文火冰糖(王文兵)的博客_文火冰糖的硅基工坊_CSDN博客
本文网址:https://blog.csdn.net/HiWangWenBing/article/details/122724044