dockerdockerfile

Docker create network should ignore existing network


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?


Solution

  • 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.