When you install Docker Engine it creates a bridge network automatically. This network corresponds to the docker0 bridge that Engine has traditionally relied on.
Docker Compose sets up a single network for your application(s) by default, adding each container for a service to the default network. Containers on a single network can reach and discover every other container on the network.
You can create multiple networks with Docker and add containers to one or more networks. Containers can communicate within networks but not across networks. A container with attachments to multiple networks can connect with all of the containers on all of those networks.
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.
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
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
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:
In fact it is very similar to the solution provided by @yktoo in comment under the answer of @Andy Triggs.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With