ERROR: 2 matches found based on name: network <nameofservice>_default is ambiguous

Viewed 21370

I was building docker image using the command

docker-compose -f "docker-compose.yml" up -d --build 

But it returns me an error

ERROR: 2 matches found based on name: network officeconverter_default is ambiguous

This is a bit clear that in my machine there are two networks with the same name trying to exists.

Question is how to remove the networks from docker networks

PS E:\repos\Github\officeconverter> docker network ls
NETWORK ID          NAME                        DRIVER              SCOPE
868c88a83bd6        bridge                      bridge              local
92f7d20ed432        officeconverter_default     bridge              local
3f96cfb7b591        officeconverter_default     bridge              local

7 Answers

The solution is simple!

just remove the networks.

like docker network rm <network Id> <space> <network Id> ....

PS E:\repos\Github\officeconverter> docker network rm 92f7d20ed432 3f96cfb7b591
92f7d20ed432
3f96cfb7b591
PS E:\repos\Github\officeconverter> docker network ls
NETWORK ID          NAME                        DRIVER              SCOPE
868c88a83bd6        bridge                      bridge              local

try: docker network prune

This will remove all your networks.

A solution that worked for me is:

docker system prune -af
docker volume prune --force.

Open cronjob with sudo cronjob -e, then, add new line

*/1 * * * * docker network prune -f

This will clean non-used networks (if any) every minute.

As others have said, you can docker network rm <network id> to actually delete them.

But if you need to choose which one to delete, you can:

$ docker network inspect d5867f0be024
[
    {
        "Name": "minikube",
        "Id": "d5867f0be024b1c81237fba1eaef3f1ff53b75e15ab84d46a13dcde53809934e",
        "Created": "2022-02-16T19:24:57.7203334-05:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        ...
    }
]

... and then see the Created time (or other metadata) to make your decision about which to delete.

In my case I needed to delete the newer one and retain the older one.

One option is to create a new network, with a different network name, and to connect your docker container to it.
There is a similar error that can happen with ambiguous bridge network names.
One guy has made a complete blog post about how to fix this.
I found it very useful as far as I am concerned, and I believe this will be useful to others in similar situation even if you deal with a network that is not bridge: https://www.jorgeanaya.dev/en/bin/docker-network-name-is-ambiguous/

Here is the short workaround from the blog post (all credit goes to Jorge Anaya):

  1. Create a new network. Use the parameter -d to specify the driver

    docker network create -d bridge [new-network-name]

  2. Disconnect the container(s) from the ambiguous network

    docker network disconnect bridge [container-name]

  3. Connect the container(s) to the new network

    docker network connect [new-network-name] [container-name]

  4. Optional. Purge our network and get rid off of the unused networks

    docker network rm $(docker network ls -q)

And that's all, now we should be able to start our containers.
Don't forget to add sudo at the beginning of each command for permissions.

Just run docker remove prune it will remove all networks and create again.

Related