Dalam istilah Docker, bridge digunakan agar memungkinkan containers yang terhubung dalam network bridge yang sama untuk berkomunikasi, sekaligus mengisolasi dari containers lain yang tidak terhubung ke network bridge tersebut.
Docker bridge driver secara otomatis menginstal aturan di mesin host sehingga containers pada network bridge yang berbeda tidak dapat berkomunikasi secara langsung satu sama lain
Use the default bridge network #
Buat 2 containers menggunaka image alpine untuk pengujian network bridge
docker run -dit --name alpine1 alpine ash
docker run -dit --name alpine2 alpine ash
Cek untuk memastikan containers sudah berjalan
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e8960d257fea alpine "ash" 7 seconds ago Up 6 seconds alpine2
3674b7e97db3 alpine "ash" 10 seconds ago Up 8 seconds alpine1
Inspect network bridge untuk melihat containers yang telah terhubung
docker network inspect bridge
...
{
"3674b7e97db3d2e3709498bc2b381c118d163c1d93492f93a898cffdab555d64": {
"Name": "alpine1",
"EndpointID": "8b8f8e59285714296d1d3ce40ee5f7c7ad8db1e4805e16e61d12b3bcd32271e5",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
},
"e8960d257fea38ee7827e7245fe1a3596e065a83018c52feaa28bea8490e6068": {
"Name": "alpine2",
"EndpointID": "29970c6b62c082038d8c8a6e5f28dba6bf88058a3b826e7769fe88870fc6ede9",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
}
}
...
Masuk ke shell pada containers alpine1 lalu coba ping
IP container alpine2.
docker exec -it alpine1 sh
/ # ping -c4 172.17.0.3
PING 172.17.0.3 (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: seq=0 ttl=64 time=0.152 ms
64 bytes from 172.17.0.3: seq=1 ttl=64 time=0.097 ms
64 bytes from 172.17.0.3: seq=2 ttl=64 time=0.087 ms
64 bytes from 172.17.0.3: seq=3 ttl=64 time=0.107 ms
Output diatas menandakan bahwa containers berhasil terhubung ke network bridge yang sama.
Sekarang coba test ping
menggunakan nama containers
/ # ping -c4 alpine2
ping: bad address 'alpine2'
Test ping
dengan nama containers tidak berhasil terhubung
Exit shell alpine1 CTRL+D
Stop dan remove containers
docker stop alpine1 alpine2
docker rm alpine1 alpine2
Use user-defined bridge networks #
Buat network bridge dengan nama net-priv
docker network create --driver bridge net-priv
List network
docker network ls
NETWORK ID NAME DRIVER SCOPE
a348ab0b6139 bridge bridge local
c066dfd4012a host host local
4bef5690ba4e net-priv bridge local
266a0a91f950 none null local
Selanjutnya buat 3 containers dengan topologi berikut.
flowchart TD
A(net-priv)
A o--o B[alpine1]
A o--o C[alpine2]
D(bridge)
D o--o E[alpine3]
docker run -dit --name alpine1 --network net-priv alpine ash
docker run -dit --name alpine2 --network net-priv alpine ash
docker run -dit --name alpine3 alpine ash
Inspect network net-priv
docker network inspect net-priv | jq '.[].Containers'
{
"394332126aedc7bca965499c853511236dd4bc0f626db25fcffd004466e89dc8": {
"Name": "alpine1",
"EndpointID": "c278cac84164596c5e0af8ac28f16fcb88ba64fd3185121cea91a2cec2cfe22d",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"da6d6c97a7b5a7d5ee458095c192b3efb4cadcd7ab6eb3bde2ee7050d91b9ff6": {
"Name": "alpine2",
"EndpointID": "64fa8a7830c5585b4ee05e4041e8de013ffd4e63b9c55c828e8a47ae94bea5a7",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
}
Inspect network bridge
docker network inspect bridge | jq '.[].Containers'
{
"8f7ad7db3a8794362a009032133b0f7d58576fe417937d78d68a19ad5eee32fe": {
"Name": "alpine3",
"EndpointID": "4853296cfe6f730deb7d368217ee886f4d036ff7f610f8192a228477cd6069b9",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
}
Masuk shell pada containers alpine1 lalu ping
ke IP containers alpine2
docker exec -it alpine1 sh
/ # ping -c4 172.18.0.3
PING 172.18.0.3 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.173 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.109 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.112 ms
64 bytes from 172.18.0.3: seq=3 ttl=64 time=0.153 ms
Selanjutnya coba ping
menggunakan nama containers
/ # ping -c4 alpine2
PING alpine2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.085 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.128 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.084 ms
64 bytes from 172.18.0.3: seq=3 ttl=64 time=0.112 ms
Kedua cara ping
berhasil terhubung. Terakhir coba ping
dengan IP dan nama containers alpine3
/ # ping -c4 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes
^C
--- 172.17.0.2 ping statistics ---
4 packets transmitted, 0 packets received, 100% packet loss
/ # ping -c4 alpine3
ping: bad address 'alpine3'