Skip to content

Instantly share code, notes, and snippets.

@pamtrak06
Last active May 13, 2020 22:19
Show Gist options
  • Save pamtrak06/da3f6e5ff6ce2e7f20ccc737492761fc to your computer and use it in GitHub Desktop.
Save pamtrak06/da3f6e5ff6ce2e7f20ccc737492761fc to your computer and use it in GitHub Desktop.
#!/bin/bash
subnet=10.193.0.0/24
iprange=10.193.0.0/16
echo -e "#!/bin/bash\n#set -e\n" > network-cleaning.sh
echo -e "#!/bin/bash\n#set -e\n" > network-recreation.sh
echo -e "#!/bin/bash\n#set -e\n" > network-links.sh
(
for c in $(docker container ls -a --format="{{ .Names }}"); do {
echo ""
echo "### [INFO]: process container [$c]..."
container_ip=$(docker container inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $c);
network=$(docker inspect --format '{{ .HostConfig.NetworkMode }}' $c);
echo -e "\t[INFO]: ip: [$container_ip]"
echo -e "\t[INFO]: net: [$network]"
if [ "default" != "$network" ]; then
status_running=$(docker container ls -a --filter="status=running" --format="{{ .Names }}"|grep $c)
echo -e "\t[INFO]: stop container [$c/ip:$container_ip] with network [$network]..."
docker container stop $c
echo -e "\t[INFO]: disconnect network [$network] from [$c/ip:$container_ip] ..."
docker network disconnect $network $c
echo "echo -e \"\t[INFO]: remove network [$network]...\"" >> network-cleaning.sh
echo -e "[ -n \"\$(docker network ls --format=\"{{ .Name }}\"|grep $network)\" ] && docker network rm $network\n" >>
network-cleaning.sh
echo "echo -e \"\t[INFO]: create network [$network] with new subnet...\"" >> network-recreation.sh
echo -e "[ -z \"\$(docker network ls --format=\"{{ .Name }}\"|grep $network)\" ] && docker network create --ip-range
=$iprange --subnet=$subnet $network\n" >> network-recreation.sh
echo "echo -e \"\t[INFO]: connect network [$network] from [$c/ip:$container_ip] ...\"" >> network-links.sh
echo -e "docker network connect $network $c\n" >> network-links.sh
#Restart only container that where previously running
if [ -n "$status_running" ]; then
echo "echo -e \"\t[INFO]: start container [$c/ip:$container_ip] with new network [$network]...\"" >> network-links
.sh
echo "docker container start $c" >> network-links.sh
fi
echo -e "\n" >> network-links.sh
fi
}; done;
echo -e "\n"
echo "### [INFO]: start removing networks..."
chmod a+x network-cleaning.sh
./network-cleaning.sh
echo -e "\n"
echo "### [INFO]: start recreating networks with new subnet [$subnet] and ip range [$iprange]..."
chmod a+x network-recreation.sh
./network-recreation.sh
echo -e "\n"
echo "### [INFO]: start previous living containers with new networks..."
chmod a+x network-links.sh
./network-links.sh
echo -e "\n"
echo "### [INFO]: show running containers"
docker container ls -a
echo "### [INFO]: show networks"
docker network ls -a
echo "### [INFO]: clean unused networks"
docker network prune -f
echo "### [INFO]: show networks after cleaning"
docker network ls -a
) 2>&1 | tee -a docker-network-subnet.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment