Skip to content

Commit

Permalink
Set IPv4 subnet only if specified or error on regular network new net…
Browse files Browse the repository at this point in the history
… creation.
  • Loading branch information
evertramos committed May 17, 2021
1 parent 25dde08 commit 3089f4e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions docker/docker-network-create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
# You must/might inform the parameters below:
# 1. Network name which should be created
# 2. [optional] (default: ) Subnet for the IPV4
# - This option is required if you are activating IPv6
# - You can set 'false' or send an empty string to this option if
# only wants to activate IPv6
# 3. [optional] (default: false) Set 'true' if IPv6 should be enabled
# 4. [optional] (default: ) Subnet for the IPV6
# in the network options
Expand All @@ -38,9 +39,10 @@ docker_network_create()
local LOCAL_NETWORK_NAME LOCAL_ENABLE_IPv4 LOCAL_ENABLE_IPv6 LOCAL_RESULT

LOCAL_NETWORK_NAME=${1:-null}
LOCAL_IPv4_SUBNET=${2:-"172.17.0.0/16"}
LOCAL_IPv4_SUBNET=${2:-null}
LOCAL_ENABLE_IPv6=${3:-false}
LOCAL_IPv6_SUBNET=${4:-"2001:db8:1:1::/112"}
LOCAL_IPv4_DEFAULT="172.172.0.0/16" # This will be used in case of error when creating a docker network

[[ $LOCAL_NETWORK_NAME == "" || $LOCAL_NETWORK_NAME == null ]] && echoerror "You must inform the network name to the function: '${FUNCNAME[0]}'"

Expand All @@ -50,12 +52,21 @@ docker_network_create()

# Create docker network
if [[ "$LOCAL_ENABLE_IPv6" == true ]]; then
if ! docker network create $LOCAL_NETWORK_NAME --ipv6 --subnet=$LOCAL_IPv6_SUBNET --subnet=$LOCAL_IPv4_SUBNET; then
ERROR_DOCKER_NETWORK_CREATE=true
if [[ $LOCAL_IPv4_SUBNET == "" || $LOCAL_IPv4_SUBNET == null || $LOCAL_IPv4_SUBNET == false ]]; then
docker network create $LOCAL_NETWORK_NAME --ipv6 --subnet=$LOCAL_IPv6_SUBNET && return
docker network create $LOCAL_NETWORK_NAME --ipv6 --subnet=$LOCAL_IPv6_SUBNET --subnet=$LOCAL_IPv4_DEFAULT && return
else
docker network create $LOCAL_NETWORK_NAME --ipv6 --subnet=$LOCAL_IPv6_SUBNET --subnet=$LOCAL_IPv4_SUBNET && return
fi
else
if ! docker network create $LOCAL_NETWORK_NAME --subnet=$LOCAL_IPv4_SUBNET; then
ERROR_DOCKER_NETWORK_CREATE=true
if [[ $LOCAL_IPv4_SUBNET == "" || $LOCAL_IPv4_SUBNET == null || $LOCAL_IPv4_SUBNET == false ]]; then
docker network create $LOCAL_NETWORK_NAME && return
docker network create $LOCAL_NETWORK_NAME --subnet=$LOCAL_IPv4_DEFAULT && return
else
docker network create $LOCAL_NETWORK_NAME --subnet=$LOCAL_IPv4_SUBNET && return
fi
fi

ERROR_DOCKER_NETWORK_CREATE=true
return
}

0 comments on commit 3089f4e

Please sign in to comment.