0

我正在为 python 使用 docker sdk

我正在创建一个这样的网络

    try:
        client.networks.create(name=network_name, check_duplicate=True)
    except docker.errors.APIError as ex:
        print(f"not recreating existing docker network: '{network_name}'")
        pass

效果很好。

我想删除这个网络。

我正在尝试这样

def remove_docker_network(network_name: str, client: docker.client.DockerClient):
    try:
        client.networks.prune(filters={"name": network_name})
    except docker.errors.APIError as ex:
        print(f"not removing non existing docker network: '{network_name}'")
        pass

但网络仍然存在。

Doc 只显示prune. 没有rm

如何正确执行?

4

1 回答 1

2

使用remove

network_name = 'hello'
client.networks.create(name=network_name, check_duplicate=True)

# docker network ls
# NETWORK ID     NAME                DRIVER    SCOPE
# 9b4262356615   bridge              bridge    local
# f6fb876e8a2b   hello               bridge    local
# 92f69ef02a1e   host                host      local
# 7dff8097025d   mailtrain_default   bridge    local
# 8326691aaf3b   none                null      local

client.networks.get(network_name).remove()

# docker network ls
# NETWORK ID     NAME                DRIVER    SCOPE
# 9b4262356615   bridge              bridge    local
# 92f69ef02a1e   host                host      local
# 7dff8097025d   mailtrain_default   bridge    local
# 8326691aaf3b   none                null      local

于 2022-01-27T16:48:26.767 回答