赞
踩
Docker 提供网络服务的方法包括两种:
在docker容器中可以运行一些网络应用,外部想要访问docker容器内的应用,可以通过-p或-P选项来指定端口映射,两个选项的区别如下所示:
# 创建一个端口随机映射到本地主机的容器
[root@localhost ~]# docker run -d -P training/webapp python app.py
fd7fe9a68463f8673fc8d73b89451d68ac2f7e7b0aeee73082cb3db45bfab476
# 查看容器信息。:::32770->5000/tcp,5000为容器端口,32770为本地主机随机映射的端口
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd7fe9a68463 training/webapp "python app.py" 4 seconds ago Up 3 seconds 0.0.0.0:32770->5000/tcp, :::32770->5000/tcp youthful_williamson
#绑定本地所有ip地址的5500端口到容器的5000端口上
[root@localhost ~]# docker run -d -p 5500:5000 training/webapp python app.py
dd400ee1a85b9ae515033cba8eaaea0996036fb8178f3ae6b26688837d534420
#0.0.0.0:5500 表示本地主机所有ip地址的5500端口
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd400ee1a85b training/webapp "python app.py" 15 seconds ago Up 13 seconds 0.0.0.0:5500->5000/tcp, :::5500->5000/tcp busy_booth
# 绑定本地端口5600上192.168.122.1的ip地址到容器的5000端口上
[root@localhost ~]# docker run -d -p 192.168.122.1:5600:5000 training/webapp python app.py
ac8e008063c701a1b0f24937ff0d0ce8ce800cf8ade9bd3a2aba09f756c8192c
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac8e008063c7 training/webapp "python app.py" 4 seconds ago Up 3 seconds 192.168.122.1:5600->5000/tcp strange_beaver
# 绑定本地主机随机端口上的192.168.122.1 ip地址到容器的5000端口上,这里的随机分配的主机端口为32768。
[root@localhost ~]# docker run -d -p 192.168.122.1::5000 training/webapp python app.py
0d2e0590b7ede08552bd42cb11bfdd47d10ebe57208de8adf6a983a4a755f5ca
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0d2e0590b7ed training/webapp "python app.py" 6 seconds ago Up 5 seconds 192.168.122.1:32768->5000/tcp vigilant_borg
[root@localhost ~]# docker run -d -p 192.168.122.1::5000 -p 3000:80 training/webapp python app.py
88ecd0c638975fca8471787be24c771643f8f939667254c5098d5c057d572fbd
# 创建一个新的Docker网络, 网络类型为bridge, 网络名称为 test-net
[root@localhost ~]# docker network create -d bridge test-net
#查看Docker网络信息
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
956cd42a27ba bridge bridge local
5091d8c0f9dd host host local
2dfe918dbe97 none null local
71c2d19d105e test-net bridge local
#运行一个容器命名为test1并连接到新建的test-net网络:
[root@localhost ~]# docker run -itd --name test1 --network test-net ubuntu /bin/bash
a84040a535dd733fbd630b845ea41e856743de95694a80776617e6d25bc55d37
#运行一个容器命名为test2并连接到新建的test-net网络:
[root@localhost ~]# docker run -itd --name test2 --network test-net ubuntu /bin/bash
7dae8252369a0ef98b4014f0324d8e618513c11724dc98508b33243a5f635d2b
#查看容器运行信息
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7dae8252369a ubuntu "/bin/bash" 2 seconds ago Up 2 seconds test1
a84040a535dd ubuntu "/bin/bash" 9 seconds ago Up 8 seconds test2
[root@localhost ~]#
apt-get update
apt install iputils-ping
# 在test1容器中ping test2
[root@localhost ~]# docker exec -it test1 /bin/bash
root@7dae8252369a:/# ping test2
PING test2 (172.18.0.2) 56(84) bytes of data.
64 bytes from test2.test-net (172.18.0.2): icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from test2.test-net (172.18.0.2): icmp_seq=2 ttl=64 time=0.061 ms
# 在test2容器中ping test1
[root@localhost ~]# docker exec -it test2 /bin/bash
root@a84040a535dd:/# ping test1
PING test1 (172.18.0.3) 56(84) bytes of data.
64 bytes from test1.test-net (172.18.0.3): icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from test1.test-net (172.18.0.3): icmp_seq=2 ttl=64 time=0.059 ms
在宿主机的 /etc/docker/daemon.json 文件中增加以下内容来设置全部容器的 DNS:
{
"dns" : [
"114.114.114.114",
"8.8.8.8"
]
}
重启 docker
查看容器的 DNS 是否生效
手动指定容器的配置
参数说明:
–rm:容器退出时自动清理容器内部的文件系统。
-h HOSTNAME 或者 --hostname=HOSTNAME: 设定容器的主机名,它会被写到容器内的 /etc/hostname 和 /etc/hosts。
–dns=IP_ADDRESS: 添加 DNS 服务器到容器的 /etc/resolv.conf 中,让容器用这个服务器来解析所有不在 /etc/hosts 中的主机名。
–dns-search=DOMAIN: 设定容器的搜索域,当设定搜索域为 .example.com 时,在搜索一个名为 host 的主机时,DNS 不仅搜索 host,还会搜索 host.example.com。
网络模式 | 简介 | 指定网络模式参数项 |
---|---|---|
bridge | Docker 默认使用 bridge 网络模式,创建一个名为 docker0 的虚拟网桥,并为每个容器分配一个 IP 地址。容器间可以通过 IP 地址相互通信 | –net = bridge(默认设置) |
host | 容器将不会虚拟出自己的网卡,配置自己的Ip等,而是使用宿主机的IP和端口。容器的网络配置与宿主机相同,可以通过宿主机的 IP 地址直接访问容器。 | –net = host |
container | 指定新创建容器与已存在的某个容器共享一个 Network Namespace。使它们可以直接使用 localhost 来进行通信,就像在同一台主机上运行的进程一样。容器之间的进程通过 lo 网卡设备通信 | –net container:已运行的容器名称 |
none | 新创建的容器不会创建自己的网卡和配置自己的IP,只有lo网卡。在none网络模式下,容器没有网络接口,无法与外部网络通信。该模式主要用于一些特殊场景,如只需要运行一个进程的容器或与网络无关的容器。 | –net =none |
# 1.查看网络信息 [root@localhost ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 152468cf8556 bridge bridge local 5091d8c0f9dd host host local 2dfe918dbe97 none null local # 2.创建bridge网络 [root@localhost ~]# docker network create --driver bridge --subnet 172.18.0.0/16 --gateway 172.18.0.1 mynet # 3.查看网络信息 docker network ls NETWORK ID NAME DRIVER SCOPE 152468cf8556 bridge bridge local 5091d8c0f9dd host host local 2dfe918dbe97 none null local 71c2d19d105e test-net bridge local # 4.查看新建网络详细信息 [root@localhost ~]# docker inspect test-net [ { "Name": "test-net", "Id": "71c2d19d105e7374630a61367432995cafb836a88e50e18f9d66b83a17a5b057", "Created": "2023-07-31T09:29:35.555918758+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.18.0.0/16", "Gateway": "172.18.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ]
在没有设置的情况下,不同网络间的容器是无法进行网络连接的。不同Docker网络之间的容器需要连接的话需要把作为调用方的容器注册一个ip到被调用方所在的网络上。需要使用docker connect命令。
分析说明:
# 1.创建四个容器(两个使用默认docker0网络,两个使用自定义网络) [root@localhost ~]# docker run -itd --name test1 ubuntu /bin/bash [root@localhost ~]# docker run -itd --name test2 --network=test-net ubuntu /bin/bash [root@localhost ~]# docker run -itd --name test3 --network=test-net ubuntu /bin/bash [root@localhost ~]# docker run -itd --name=test5 ubuntu /bin/bash # 2.测试网络之间的容器连通性 # 容器 test1 不通 test2、test3, 但能ping通同是docker0网络的test5 [root@localhost ~]# docker exec -it test1 /bin/bash root@620e6715cb0d:/# ping test2 ping: test2: Name or service not known root@620e6715cb0d:/# ping test3 ping: test3: Name or service not known root@620e6715cb0d:/# ping 172.17.0.3 --- 172.17.0.2 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2004ms root@620e6715cb0d:/# ping 172.18.0.3 --- 172.18.0.2 ping statistics --- 48 packets transmitted, 0 received, 100% packet loss, time 47156ms # 容器test2通test3,但不通test1 [root@localhost ~]# docker exec -it test2 /bin/bash root@a454d400cc2a:/# ping test3 PING test3 (172.18.0.3) 56(84) bytes of data. 64 bytes from test3.test-net (172.18.0.3): icmp_seq=1 ttl=64 time=0.047 ms 64 bytes from test3.test-net (172.18.0.3): icmp_seq=2 ttl=64 time=0.152 ms --- test3 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1006ms rtt min/avg/max/mdev = 0.047/0.099/0.152/0.052 ms root@a454d400cc2a:/# ping test1 ping: test1: Name or service not known root@a454d400cc2a:/#
设置默认bridge网络的test1容器连接到自定义网络test-net
测试连通性
[root@localhost ~]# docker exec -it test1 /bin/bash
root@620e6715cb0d:/# ping test2
PING test2 (172.18.0.2) 56(84) bytes of data.
64 bytes from test2.test-net (172.18.0.2): icmp_seq=1 ttl=64 time=0.055 ms
64 bytes from test2.test-net (172.18.0.2): icmp_seq=2 ttl=64 time=0.053 ms
查看test1网络信息
root@620e6715cb0d:/# 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 5144 bytes 28870205 (28.8 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3887 bytes 215608 (215.6 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.0.4 netmask 255.255.0.0 broadcast 172.18.255.255 ether 02:42:ac:12:00:04 txqueuelen 0 (Ethernet) RX packets 29 bytes 2154 (2.1 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 21 bytes 1498 (1.4 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 52 bytes 3941 (3.9 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 52 bytes 3941 (3.9 KB)
```
[root@localhost ~]# netstat -nap|grep 5000
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 8290/python
[root@localhost ~]# ps -ef|grep 8290
root 8290 8268 0 14:44 pts/0 00:00:00 python app.py
root 8387 3781 0 14:48 pts/0 00:00:00 grep --color=auto 8290
```
docker network create --driver overlay test-overlay-net
docker network create -d macvlan --subnet= --gateway= -o parent=<parent_interface> test-macvlan-net
docker run --name container-name --network test-macvlan-net test-image
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。