#帮助命令后显示下面信息
[root@localhost /]# docker network --help
Usage: docker network COMMAND
Manage networks
Options:
--help Print usage
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.
docker network create mynet
然后pull两个镜像使用新创建的网络
#pull之前先设置docker镜像加速,国外pull速度很慢,参考:
https://blog.csdn.net/wangyue23com/article/details/110194399
docker pull redis
docker pull nginx
#运行redis容器
docker run -itd --name redis --network mynet --network-alias redis -p 6379:6379 redis
#运行nginx容器
docker run -d --name nginx -p 80:80 --network mynet --network-alias nginx --privileged=true -v /home/wwwroot:/home/wwwroot -v /home/wwwlogs:/home/wwwlogs nginx
运行容器时候加入命令 --network mynet --network-alias redis,
代表当然容器要加入到mynet局域网络中
#查看mynet的详细信息,发现redis和nginx已在Containers容器内,属于当前mynet网络
[root@localhost /]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "8453b7b1d246f1ad54e27033928bbb8e7842ca96b4315fad720688af206b69c6",
"Created": "2020-12-13T22:50:55.566207522-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,
"Containers": {
"87ff63bb819a823578bed89a67527009d9fee31a92de9d50e5d635b97cc18664": {
"Name": "redis",
"EndpointID": "19a04279300025dc936d206a287b1e31d04b339298b445694452a5f4326f7051",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"c75ff4ac770fd04a1c6daa05a82533555038e3b0f8e15c3b1ee29e26c14a0131": {
"Name": "nginx",
"EndpointID": "4ccc014edcfd7be443c75491cc32aa0745561cfd45903e204e8d3e7afa94a4fe",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
#进入redis命令行,使用ping nginx命令验证能够连接成功**使用ping命令之前,需要给docker安装ping,自行百度**
docker exec -it redis bash
root@822f249e7d90:/# ping nginx
PING nginx (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: icmp_seq=0 ttl=64 time=0.071 ms
64 bytes from 172.18.0.3: icmp_seq=1 ttl=64 time=0.100 ms
同样也可以不指定网络,直接启动容器,然后使用docker network connect接入网络。
#将容器删掉重新创建
docker stop nginx
docker rm -f nginx
docker stop redis
docker rm -f redis
#运行nginx和docker容器
docker run -itd --name redis -p 6379:6379 redis
docker run -d --name nginx -p 80:80 --privileged=true -v /home/wwwroot:/home/wwwroot -v /home/wwwlogs:/home/wwwlogs nginx
使用docker network connect命令连接网络
#加入nginx到mynet网络
[root@localhost /]# docker network connect mynet nginx
#加入redis到mynet网络
[root@localhost /]# docker network connect mynet redis
#查看mynet网络包含的容器,会发现两个容器都加入当前网络了,后面使用ping命令是能够ping通容器名称的。
[root@localhost /]# docker network inspect mynet
#将nginx移除mynet局域网络
docker network disconnect mynet nginx
使用docker network 很方便的维护和管理docker网络,方便容器之间ip和端口交互。