Docker create network should ignore existing network

DockerDockerfile

Docker Problem Overview


My docker containers are running in a local network, called my_local_network. To assure the network exists, every build script starts with:

docker network create --driver bridge my_local_network

This works fine. If the network does not exist, it is created, if not, nothing happens. Except for the error message:

Error response from daemon: network with name my_local_network already exists

Is there a way to tell docker only to create the network if it doesn't exist?

Docker Solutions


Solution 1 - Docker

Currently there is no way to force it OR ignore it but you can get rid of this problem using shell -

docker network create --driver bridge my_local_network || true

This way whenever your build script executes, if there is no network it will create one else it will return true without any command failure so that rest of the build script can execute.

Solution 2 - Docker

Building on @AndyTriggs' answer, a neat (and correct) solution would be:

docker network inspect my_local_network >/dev/null 2>&1 || \
    docker network create --driver bridge my_local_network

Solution 3 - Docker

You can first test for the existence of the network, and create it if it doesn't exist. For example:

docker network ls|grep my_local_network > /dev/null || echo "network does not exist"

Replace the echo with your network create command:

docker network ls|grep my_local_network > /dev/null || docker network create --driver bridge my_local_network

Solution 4 - Docker

You can do it also in this way:

NETWORK_NAME=my_local_network
if [ -z $(docker network ls --filter name=^${NETWORK_NAME}$ --format="{{ .Name }}") ] ; then 
     docker network create ${NETWORK_NAME} ; 
fi

Advantages:

  1. Regexp prevents omitting network creation in case of existing network with similar name.
  2. Errors in docker commands will not pass silently.

In fact it is very similar to the solution provided by @yktoo in comment under the answer of @Andy Triggs.

Solution 5 - Docker

You can use grep simply like below, without printing out any errors:

if [[ "$(docker network ls | grep "${networkName}")" != "" ]] ; then
    docker network rm "${networkName}"
fi

docker network create --subnet=172.10.0.0/16 "${networkName}"

In this case, I assume that already created one network can have a different subnet configuration than mine.

For your case will be:

if [[ "$(docker network ls | grep "${networkName}")" == "" ]] ; then
    docker network create "${networkName}"
fi

Solution 6 - Docker

If you can use PowerShell

$networkName = "some name here"

if (docker network ls | select-string $networkName -Quiet )
{
	Write-Host "$networkName already created"
} else {
	docker network create $networkName
}

Tried on PS core 7.0.3 - this version is available for Linux.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnna Ira HurnausView Question on Stackoverflow
Solution 1 - Dockervivekyad4vView Answer on Stackoverflow
Solution 2 - DockeryktooView Answer on Stackoverflow
Solution 3 - DockerAndy TriggsView Answer on Stackoverflow
Solution 4 - DockerGrzegorz SkupienView Answer on Stackoverflow
Solution 5 - Dockerwisniewski28View Answer on Stackoverflow
Solution 6 - DockerBakudanView Answer on Stackoverflow