From 5c2ab9fc9ef995e97a2a618ec0dc49ace89387c2 Mon Sep 17 00:00:00 2001 From: Abbas <53353647+Abbas-b-b@users.noreply.github.com> Date: Thu, 1 Dec 2022 15:50:33 +0330 Subject: [PATCH] Dynamically choose the Docker network subnet --- generate_config.sh | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/generate_config.sh b/generate_config.sh index 70dc58877d..b76a9045fc 100755 --- a/generate_config.sh +++ b/generate_config.sh @@ -158,6 +158,24 @@ if [ ! -z "${MAILCOW_BRANCH}" ]; then git_branch=${MAILCOW_BRANCH} fi +if [[ $(docker network ls -q \ + | xargs docker network inspect --format $'{{range .IPAM.Config}}{{.Subnet}}\n{{end}}' \ + | grep -qsP '172\.22.*' )$? \ + -eq 0 ]]; then + + max_octet=$(docker network ls -q \ + | xargs -I {} docker network inspect {} --format $'{{range .IPAM.Config}}{{.Subnet}}\n{{end}}' \ + | grep -P '172\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+$' \ + | sort \ + | tail -n 1 \ + | sed -e 's~\(172\)\.\([0-9]\+\).*~\2~g') + + DOCKER_NETWORK="172.$(($max_octet+1)).1" +else + DOCKER_NETWORK='172.22.1' +fi + + git fetch --all git checkout -f $git_branch @@ -361,7 +379,7 @@ LOG_LINES=9999 # Internal IPv4 /24 subnet, format n.n.n (expands to n.n.n.0/24) # Use private IPv4 addresses only, see https://en.wikipedia.org/wiki/Private_network#Private_IPv4_addresses -IPV4_NETWORK=172.22.1 +IPV4_NETWORK=${DOCKER_NETWORK} # Internal IPv6 subnet in fc00::/7 # Use private IPv6 addresses only, see https://en.wikipedia.org/wiki/Private_network#Private_IPv6_addresses