之前用過docker-compose啟動Redis Cluster,而docker-compose.yaml需要ip這個環境變數,在Mac可以透過ipconfig指令取得IP值設給ip變數啟動Redis Cluster
ip=$(ipconfig getifaddr en0) docker-compose up -d --build
但是CentOS 8,還有Rocky Linux 9均沒有ipconfig指令,而只有ip指令,先看ip指令怎麼下,而且還必須帶參數:
[rocky@devops01 ~]$ ip a show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1442 qdisc fq_codel state UP group default qlen 1000
link/ether fa:16:3e:12:99:d4 brd ff:ff:ff:ff:ff:ff
altname enp0s3
altname ens3
inet 10.0.0.127/24 brd 10.0.0.255 scope global dynamic noprefixroute eth0
valid_lft 41958sec preferred_lft 41958sec
inet6 fe80::f816:3eff:fe12:99d4/64 scope link
valid_lft forever preferred_lft forever
目的是要取得inet後的IP值:10.0.0.127,所以要透過grep指令取得:
[rocky@devops01 ~]$ ip a show dev eth0 | grep -oP "\K[^inet ](\d{1,3}\.){3}\d{1,3}(?=/)"
10.0.0.127
grep的正規表達有點怪,\K[...]的作用相當於(?!...)
所以在CentOS 8上啟動docker-compose的Redis Cluster變成了:
ip=$(ip a show dev eth0 | grep -oP "\K[^inet ](\d{1,3}\.){3}\d{1,3}(?=/)") docker-compose up -d --build