์ปจํ ์ด๋์ ๋คํธ์ํฌ์ ๋ํด์ ์์๋ณด๊ณ , ์ค์ตํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
Docker Network ๋ ๋ธ๋ฆฌ์ง(Bridge),ํธ์คํธ(host), ์ปจํ
์ด๋(container), ๋
ผ(none) ์ด๋ ๊ฒ 4๊ฐ์ง๋ก ๊ตฌ๋ถ๋ฉ๋๋ค.
์๋ 3๊ฐ์ง ๋ฐฉ์๋ง ๋ณด์ด๋๋ฐ, ์ปจํ
์ด๋ ๋ฐฉ์์ ๋ณ๋๋ก ๋๋ผ์ด๋ฒ๋ฅผ ์ฌ์ฉํ์ง ์๊ธฐ๋๋ฌธ์ ๋์ค์ง ์์ต๋๋ค.
[root@docker /]# docker network ls
NETWORK ID NAME DRIVER SCOPE
103b63dc3b48 bridge bridge local
db3d9f5b68d6 host host local
3a8a3fc8dbd7 none null local
๊ฐ ์ค์ต์ ํตํด ์ด๋ป๊ฒ ๋ค๋ฅธ์ง ์์๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
1. Docker Network - Bridge
docker์ ๊ธฐ๋ณธ ๋คํธ์ํฌ ์ต์
์ Bridge์
๋๋ค.
์ปจํ
์ด๋๊ฐ ์์ฑ๋๋ฉด ๊ฐ ์ปจํ
์ด๋๋ง๋ค ๊ณ ์ ํ Network Namespace ์์ญ์ด ์์ฑ๋๋ฉฐ,
docker0 bridge์ ์ปจํ
์ด๋์ ์ธํฐํ์ด์ค๊ฐ ํ๋์ฉ ๋ฐ์ธ๋ฉ ๋๋ ๊ตฌ์กฐ์
๋๋ค.
1-1) ๋ธ๋ฆฟ์ง ๋คํธ์ํฌ ์์ฑ
์ด๋ฒ ๋จ๊ณ์์๋ ๋ธ๋ฆฟ์ง ๋คํธ์ํฌ๋ฅผ ์์ฑํด๋ณด๊ฒ ์ต๋๋ค. :)
๋ธ๋ฆฟ์ง ๋คํธ์ํฌ ์์ฑํ ๋ ๋ฃ์ด์ฃผ๋ ์ต์
์
๋๋ค.
--driver: ๋คํธ์ํฌ ๊ด๋ฆฌ ๋๋ผ์ด๋ฒ
--subnet: ๋คํธ์ํฌ ๋ฒ์ CIDR
--ip-range: ์ปจํ
์ด๋์ ํ ๋น๋ ์ค์ IP์ฃผ์ ๋ฒ์
--gateway: ์๋ธ๋ท ๊ฒ์ดํธ์จ์ด ์ง์ (๋ณ๋๋ก ์ง์ ํ์ง ์์์ ์์๋ก ์ง์ )
[root@docker /]# docker network create --driver bridge --subnet=10.0.0.0/16 --ip-range=10.0.1.0/24 --gateway=10.0.1.1 vswitch01
5b89ca2c84aca4adaac7d687735ac835829f30117290e813e714d9e22f18e3a9
Docker Network ๋ช ๋ น์ด๋ก ๋คํธ์ํฌ ๊ตฌ์ฑ ์ ๋ณด๋ฅผ ํ์ธํฉ๋๋ค. ๋ฐฉ๊ธ ์์ฑํ vswitch01 ๋คํธ์ํฌ๊ฐ ๋ณด์ด๋ค์.
[root@docker /]# docker network ls
NETWORK ID NAME DRIVER SCOPE
103b63dc3b48 bridge bridge local
db3d9f5b68d6 host host local
3a8a3fc8dbd7 none null local
5b89ca2c84ac vswitch01 bridge local
2-2) ๋ธ๋ฆฟ์ง ๋คํธ์ํฌ ์ค์ต
๋จผ์ ์ค์ต์ฉ๋์ ๋์ปค ์ปจํ ์ด๋ Centos1, Centos2๋ฅผ ์์ฑํ๊ฒ ์ต๋๋ค.
[root@docker /]# docker run --name centos1 --hostname centos1 --network vswitch01 -itd centos
a373e969465d70a86d6ff0fc71f59b95a211db2c38e7c14ff43cf70594085d0b
[root@docker /]# docker run --name centos2 --hostname centos2 --network vswitch01 -itd centos
23c5e5260ea08a12377fd8cfee14deaacc9be47ba42073510c3328a5bb873cfd
Centos1 ์ปจํ ์ด๋์ ์ ์ํด์ ํต์ ํ ์คํธ๋ฅผ ์งํํด๋ด ์๋ค.
๋จผ์ , centos1 ์ปจํ ์ด๋์ ์ ์ํฉ๋๋ค.
[root@docker /]# docker exec -it centos1 /bin/bash
์ด์ ip a ๋ช ๋ น์ด๋ก ๋คํธ์ํฌ ์ ๋ณด๋ฅผ ํ์ธํฉ๋๋ค. 10.0.1.0 IP๊ฐ ํ ๋น๋์๋ค์.
[root@centos1 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
25: eth0@if26: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:0a:00:01:00 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.0.1.0/16 brd 10.0.255.255 scope global eth0
valid_lft forever preferred_lft forever
ping ์ผ๋ก centos2 ์ปจํ
์ด๋์ ํต์ ์ด ๋๋์ง ํ์ธํฉ๋๋ค.
์๋๋ค์ :)
[root@centos1 /]# ping -c3 centos2
PING centos2 (10.0.1.2) 56(84) bytes of data.
64 bytes from centos2.vswitch01 (10.0.1.2): icmp_seq=1 ttl=64 time=0.084 ms
64 bytes from centos2.vswitch01 (10.0.1.2): icmp_seq=2 ttl=64 time=0.061 ms
64 bytes from centos2.vswitch01 (10.0.1.2): icmp_seq=3 ttl=64 time=0.053 ms
--- centos2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.053/0.066/0.084/0.013 ms
[root@centos1 /]# exit
์ด๋ฒ์๋ centos2 ์ปจํ ์ด๋์ ์ ์ํฉ๋๋ค.
[root@docker /]# docker exec -it centos2 /bin/bash
๋ง์ฐฌ๊ฐ์ง๋ก, Centos2 ์ปจํ ์ด๋์ ์ค์ ๋ ๋คํธ์ํฌ๋ฅผ ํ์ธํฉ๋๋ค. 10.0.1.2 IP๊ฐ ํ ๋น๋์์ต๋๋ค :)
[root@centos2 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
27: eth0@if28: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:0a:00:01:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.0.1.2/16 brd 10.0.255.255 scope global eth0
valid_lft forever preferred_lft forever
์ด์ centos1 ์ปจํ
์ด๋์ ํต์ ์ด ์ ๋๋์ง ํ์ธํด๋ด
๋๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก ์์ฃผ ์ ๋๋ค์ :)
[root@centos2 /]# ping -c3 centos1
PING centos1 (10.0.1.0) 56(84) bytes of data.
64 bytes from centos1.vswitch01 (10.0.1.0): icmp_seq=1 ttl=64 time=0.049 ms
64 bytes from centos1.vswitch01 (10.0.1.0): icmp_seq=2 ttl=64 time=0.051 ms
64 bytes from centos1.vswitch01 (10.0.1.0): icmp_seq=3 ttl=64 time=0.045 ms
--- centos1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 0.045/0.048/0.051/0.006 ms
[root@centos2 /]# exit
2. Docker Network - Host
Host๋ฐฉ์์ ์ปจํ
์ด๋๋ฅผ ์ค์ํ ๋ Host์ ๋คํธ์ํฌ๋ฅผ ๊ฐ์ด ์ฌ์ฉํ๋๋ก ํ๋ ์ต์
์
๋๋ค.
์ด๋ ๊ฒ ์ค์ ํ๋ฉด ์ปจํ
์ด๋๋ ๋
๋ฆฝ์ ์ธ ๋คํธ์ํฌ์์ญ์ ๊ฐ์ง ์๊ณ ํธ์คํธ์ ๋คํธ์ํฌ๋ฅผ ๊ณต์ ํ์ฌ ์ฌ์ฉํฉ๋๋ค.
๋ฐ๋ก ํ
์คํธ ํด๋ณด๊ฒ ์ต๋๋ค.
๋จผ์ , network host ๋ฐฉ์์ผ๋ก ์ปจํ ์ด๋๋ฅผ ์คํํฉ๋๋ค.
[root@docker /]# docker run --name centos3 --hostname cent3 --network host -itd centos
11993b72216806d73a0333c0d241070f26c7a1b0a91ceacdc880686cfce7c5ce
์ปจํ ์ด๋ ๋ชฉ๋ก์ ํ์ธํ์ฌ ์ ์์ฑ๋์๋์ง ํ์ธํฉ๋๋ค.
[root@docker /]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11993b722168 centos "/bin/bash" 10 seconds ago Up 10 seconds centos3
23c5e5260ea0 centos "/bin/bash" 8 minutes ago Up 8 minutes centos2
a373e969465d centos "/bin/bash" 8 minutes ago Up 8 minutes centos1
์ปจํ ์ด๋์ ip a ๋ช ๋ น์ด๋ฅผ ์คํ์์ผ ์ถ๋ ฅ ๋ด์ฉ์ ์ดํด๋ด ๋๋ค. ํธ์คํธ์ปดํจํฐ์ ์ค์ ๋ด์ฉ์ด ๊ฐ์ต๋๋ค :)
[root@docker /]# docker exec -it centos3 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc mq state UP group default qlen 1000
link/ether 02:c2:72:d9:ee:7c brd ff:ff:ff:ff:ff:ff
inet 172.31.3.201/20 brd 172.31.15.255 scope global dynamic ens5
valid_lft 2840sec preferred_lft 2840sec
inet6 fe80::c2:72ff:fed9:ee7c/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:6e:fa:50:8e brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:6eff:fefa:508e/64 scope link
valid_lft forever preferred_lft forever
22: br-5b89ca2c84ac: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:93:30:94:f3 brd ff:ff:ff:ff:ff:ff
inet 10.0.1.1/16 brd 10.0.255.255 scope global br-5b89ca2c84ac
valid_lft forever preferred_lft forever
inet6 fe80::42:93ff:fe30:94f3/64 scope link
valid_lft forever preferred_lft forever
26: vethd7504ec@if25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-5b89ca2c84ac state UP group default
link/ether 72:39:41:ba:d2:38 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::7039:41ff:feba:d238/64 scope link
valid_lft forever preferred_lft forever
28: veth8f37b6f@if27: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-5b89ca2c84ac state UP group default
link/ether 72:2d:e5:33:45:9f brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::702d:e5ff:fe33:459f/64 scope link
valid_lft forever preferred_lft forever
์ธ๋ถ ๋คํธ์ํฌ ์ฐ๊ฒฐ๋ ์ ๋๋์ง ํ์ธํด๋ด ๋๋ค.
[root@docker /]# docker exec -it centos3 ping -c3 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=106 time=30.1 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=106 time=30.1 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=106 time=30.1 ms
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 4ms
rtt min/avg/max/mdev = 30.127/30.132/30.138/0.141 ms
docker inspect ๋ช
๋ น์ด๋ฅผ ์ด์ฉํ์ฌ host ๋คํธ์ํฌ๋ฅผ ํ์ธํด๋ณด๊ฒ ์ต๋๋ค.
๋ช
๋ น์ด ์ถ๋ ฅ๊ฒฐ๊ณผ์ "Containers" Centos3์ ํ์ธํ ์ ์์ต๋๋ค :)
[root@docker /]# docker network inspect host
[
{
"Name": "host",
"Id": "db3d9f5b68d68428cc20015039a324e3929aa928dc3ddf50488b02e827d91efc",
"Created": "2020-09-28T02:21:10.637717387Z",
"Scope": "local",
"Driver": "host",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": []
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"11993b72216806d73a0333c0d241070f26c7a1b0a91ceacdc880686cfce7c5ce": {
"Name": "centos3",
"EndpointID": "468e3ffa63589a1dfa5a3f98cbc2f55797cdc6f97ee9045bc4ab882127032e9f",
"MacAddress": "",
"IPv4Address": "",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
3. Docker Network - Container
๊ธฐ์กด ์กด์ฌํ๋ ์ปจํ ์ด๋์ ๋คํธ์ํฌ ํ๊ฒฝ์ ๊ณต์ ํ๋ ์ต์ ์ ๋๋ค.
๋ฐ๋ก ์ค์ต ์งํํด๋ณด๊ฒ ์ต๋๋ค.
์ผ๋จ, ์ปจํ
์ด๋์ ๋ชฉ๋ก์ ํ์ธํด๋ด
๋๋ค.
[root@docker /]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11993b722168 centos "/bin/bash" 5 minutes ago Up 5 minutes centos3
23c5e5260ea0 centos "/bin/bash" 14 minutes ago Up 14 minutes centos2
a373e969465d centos "/bin/bash" 14 minutes ago Up 14 minutes centos1
์ปจํ
์ด๋ ๋คํธ์ํฌ ์ต์
( --net=container:<์ปจํ
์ด๋ID>)์ ์ฃผ๊ณ , ์ปจํ
์ด๋๋ฅผ ์คํํฉ๋๋ค.
์ฌ๊ธฐ์์๋ centos4 ์ปจํ
์ด๋๋ฅผ ์คํ์ํค๋ฉด์, centos1 ์ปจํ
์ด๋์ ๋คํธ์ํฌ๋ฅผ ์ฐ๊ฒ ๋ค๊ณ ์ต์
์ ๋ฃ์ด์คฌ์ต๋๋ค.
[root@docker /]# docker run --name centos4 --net=container:a373e969465d -dit centos
20ff7f9ff27049e4596bf4bfb2242935386f28f3f8380f031b44fb38688a43c3
์๋ก ์์ฑํ ์ปจํ
์ด๋ centos4 ์ ๋คํธ์ํฌ ์ค์ ์ ํ์ธํฉ๋๋ค. Centos4์ ์ค์ ๋ IP๊ฐ Centos1์ IP์ ๊ฐ์ต๋๋ค.
[root@docker /]# docker exec -it centos4 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
25: eth0@if26: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:0a:00:01:00 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.0.1.0/16 brd 10.0.255.255 scope global eth0
valid_lft forever preferred_lft forever
inspect ๋ช ๋ น์ด๋ฅผ ํตํด ํ์ธํด๋ณด๋ฉด, centos4์ปจํ ์ด๋๋ ๋ค๋ฅธ ์ปจํ ์ด๋์ ๋คํธ์ํฌ๋ฅผ ๊ณต์ ํ๋ฏ๋ก ๋คํธ์ํฌ ์ค์ ์ด ์์ต๋๋ค.
[root@docker /]# docker container inspect centos4
.. ์๋ต..
"NetworkSettings": {
"Bridge": "",
"SandboxID": "",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {}
}
.. ์๋ต..
4. Docker Network - None
None Network ์ต์
์ผ๋ก ์ปจํ
์ด๋๋ฅผ ์์ฑํ๋ฉด, ๊ฒฉ๋ฆฌ๋ ์ปจํ
์ด๋๊ฐ ์์ฑ๋ฉ๋๋ค.
lookback์ธํฐํ์ด์ค๋ง ๊ฐ์ง๋ฉฐ, ํต์ ์ ์ํ ์ธํฐํ์ด์ค๋ฅผ ๊ฐ์ง์ง ์์ผ๋ฉฐ, ์ธ๋ถ ํต์ ์ด ๋ถ๊ฐ๋ฅํ ์ํ์
๋๋ค.
none ๋คํธ์ํฌ๋ก ์ปจํ ์ด๋๋ฅผ ์คํํฉ๋๋ค.
[root@docker /]# docker run --name centos5 --net=none -dit centos
cb3738c93d86a6d24ed014ee85181a7917cc59102d27075714364ac0e6054b57
์ปจํ ์ด๋์ ๋คํธ์ํฌ ์ค์ ์ ๋ณด ํ์ธ ์ loopback ๋คํธ์ํฌ ์ธ ๋ค๋ฅธ ๋คํธ์ํฌ๋ ์ค์ ๋์ด์์ง ์์ต๋๋ค.
[root@docker /]# docker exec -it centos5 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
[root@docker /]#
๊ณ ์ํ์ จ์ต๋๋ค :)