本文我們簡(jiǎn)單說(shuō)一下姑荷,容器之間是怎么相互他通訊的盒延,假設(shè)我們一個(gè)容器只有php,一個(gè)容器只有nginx鼠冕,一個(gè)容器只有mysql或者redis添寺,我們想要php容器去鏈接mysql或者redis應(yīng)該怎么做呢
首先,如果我們?cè)跊](méi)有指定網(wǎng)絡(luò)的情況一下懈费,一般容器是會(huì)使用默認(rèn)的網(wǎng)絡(luò)计露,也就是bridge ,
我們看下docker網(wǎng)絡(luò)列表
[root@work home]# docker network list
NETWORK ID NAME DRIVER SCOPE
5927b735123e bridge bridge local
c71ab2fd1bcf host host local
707911944ab8 none null local
然后啟動(dòng)3個(gè)docker
[root@work home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e18e2880b5e centos:7.6.1810 "/bin/bash" 27 minutes ago Up 27 minutes centostest
fbfd416582a0 nginx:latest "/docker-entrypoint.…" 37 minutes ago Up 37 minutes 80/tcp nginxtest
c3e05f45b3a1 php:7.4.27-fpm "docker-php-entrypoi…" 48 minutes ago Up 48 minutes 9000/tcp test
我們隨意進(jìn)入一個(gè)容器里面
[root@work home]# docker exec -it centostest /bin/bash
[root@4e18e2880b5e /]# ping nginxtest
ping: nginxtest: Name or service not known
發(fā)現(xiàn)是無(wú)法識(shí)別的,我們可以通過(guò)一下操作來(lái)創(chuàng)建一個(gè)網(wǎng)絡(luò)(因?yàn)槟J(rèn)網(wǎng)絡(luò)不帶dns)
#創(chuàng)建網(wǎng)絡(luò)
docker network create jinan
#將容器nginxtest加入到網(wǎng)絡(luò)中
docker network connect jinan nginxtest
#將容器centostest加入到網(wǎng)絡(luò)中
docker network connect jinan centostest
#將容器test加入到網(wǎng)絡(luò)中
docker network connect jinan test
現(xiàn)在可以先看下網(wǎng)絡(luò)列表
[root@work home]# docker network list
NETWORK ID NAME DRIVER SCOPE
5927b735123e bridge bridge local
c71ab2fd1bcf host host local
b48d23bb5492 jinan bridge local
707911944ab8 none null local
可以查看下使用該網(wǎng)絡(luò)的有哪些容器
[root@work home]# docker network inspect jinan
[
{
"Name": "jinan",
"Id": "b48d23bb5492d6fa2b02042dabbca54715d4e0070b8104bff6ece718f8b5a29d",
"Created": "2022-01-14T17:57:39.983008369+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": {
"4e18e2880b5e31b6f1cc4f1fbcf837a52f311e8d5b71d516f964701ae0fba7ad": {
"Name": "centostest",
"EndpointID": "9ce4e741a8ac48f5d58d85819da2b68dc8cce7820a714ba39f0daa7e555e198f",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
},
"c3e05f45b3a16d77d98c57feaff9e5a9fd7e3ca6e4a9d4af0853fc2538cab82f": {
"Name": "test",
"EndpointID": "6594cd35fa6594027ea229ee93f32e39c1e0bd02d8050c65af8a0c98e319671a",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
},
"fbfd416582a011079813442553b3b9e5bfdc97aeda4d1da5100adaa0813c681e": {
"Name": "nginxtest",
"EndpointID": "701387ca6762be60cef0702ff0dc0e4c51230f7d80c2dae3870b64224eae8ec6",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
從Containers可以看到剛加入的三個(gè)容器都在這個(gè)網(wǎng)絡(luò)里
進(jìn)入任意一個(gè)容器中
[root@work home]# docker exec -it centostest /bin/bash
[root@4e18e2880b5e /]# ping nginxtest
PING nginxtest (172.18.0.2) 56(84) bytes of data.
64 bytes from nginxtest.jinan (172.18.0.2): icmp_seq=1 ttl=64 time=0.043 ms
64 bytes from nginxtest.jinan (172.18.0.2): icmp_seq=2 ttl=64 time=0.050 ms
64 bytes from nginxtest.jinan (172.18.0.2): icmp_seq=3 ttl=64 time=0.051 ms
64 bytes from nginxtest.jinan (172.18.0.2): icmp_seq=4 ttl=64 time=0.061 ms
64 bytes from nginxtest.jinan (172.18.0.2): icmp_seq=5 ttl=64 time=0.052 ms
OK 已經(jīng)實(shí)現(xiàn)了容器之間的通訊了憎乙,已經(jīng)創(chuàng)建了網(wǎng)絡(luò)的票罐,以后啟動(dòng)容器可以通過(guò) --network=jinan來(lái)直接加入到j(luò)inan網(wǎng)絡(luò)中