82

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?

Anna Ira Hurnaus
  • 1,680
  • 3
  • 19
  • 29

9 Answers9

107

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
lucidyan
  • 3,575
  • 2
  • 22
  • 24
yktoo
  • 2,696
  • 1
  • 23
  • 35
  • 3
    Can someone tell me what the `2>&1` part means? :) – Sam Nov 30 '19 at 11:26
  • 4
    @Sam it's a redirection of `stderr` into `stdout`, that is, into `/dev/null`. – yktoo Nov 30 '19 at 19:00
  • 2
    Useful variation: with `docker network inspect my_local_network --format {{.Id}} 2>/dev/null || docker network create --driver bridge my_local_network` the command will return the network id in each case. – not2savvy Jul 14 '21 at 12:41
87

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.

vivekyad4v
  • 13,321
  • 4
  • 55
  • 63
  • 36
    This has the disadvantage if `docker network create ...` itself fails, your script would believe evertyhing is fine. – SubOptimal Mar 11 '19 at 08:56
  • Yeah, but if the script then tries to use the network (e.g. starting a container in it) you will still get an error. – Daniel Darabos Apr 28 '22 at 13:14
18

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
Andy Triggs
  • 1,286
  • 12
  • 17
  • 8
    `docker network inspect my_local_network >/dev/null || docker network create --driver bridge my_local_network`, no need to grep. Your command will also misbehave if you have a network named like `my_local_network_1`. – yktoo Aug 10 '18 at 10:53
  • 2
    Thanks @yktoo - that's much tidier. I had considered inspect but for some reason hadn't thought to redirect it to /dev/null at that point, and didn't want the side effect of a load of inspection info. – Andy Triggs Aug 10 '18 at 14:41
  • 1
    @yktoo you might want to turn that into an answer since it adds a different solution to all the other answers which have already been provided. – Dirk Oct 29 '18 at 15:51
12

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.

4

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
2

For an option that potentially works for Windows and Linux:

docker network inspect my-network || docker network create my-network
Lindsay-Needs-Sleep
  • 1,304
  • 1
  • 15
  • 26
1

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.

Bakudan
  • 19,134
  • 9
  • 53
  • 73
1

A slightly cleaner version of @wisniewski28 answer:

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

For those who'd like a "chatty" version, this tells us what's happening without indicating it's an error if the network already exists.

NETWORK="my_local_network"
if docker network inspect ${NETWORK} > /dev/null 2>&1
then
    echo "Network '${NETWORK}' already exists"
else
    echo "Network '${NETWORK}' doesn't exist; creating it"
    docker network create ${NETWORK} > /dev/null
fi
Matthew Walker
  • 2,527
  • 3
  • 24
  • 30