Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure no duplicates for docker networks, fixes #5193 #5508

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions pkg/ddevapp/ddevapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2558,10 +2558,8 @@ func (app *DdevApp) Stop(removeData bool, createSnapshot bool) error {
}

// Remove current project network
// Working around duplicate network creation problem apparently caused by
// https://github.com/docker/compose/issues/6532#issuecomment-769263484
// see https://github.com/ddev/ddev/issues/5193 and
// https://github.com/ddev/ddev/pull/5305
// Working around duplicate network creation problem
// see https://github.com/ddev/ddev/pull/5508
dockerutil.RemoveNetworkWithWarningOnError(os.Getenv("COMPOSE_PROJECT_NAME") + "_default")

return nil
Expand Down
11 changes: 7 additions & 4 deletions pkg/ddevapp/poweroff.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ func PowerOff() {
util.Error("Failed to remove ddev-ssh-agent: %v", err)
}

// Remove all global networks created with DDEV
// Remove global DDEV default network
dockerutil.RemoveNetworkWithWarningOnError(dockerutil.NetName)

// Remove all external networks created with DDEV
// This is needed for compatibility if we decide to undo the changes
// made for the external project networks
// see https://github.com/ddev/ddev/pull/5508
removals, err := dockerutil.FindNetworksWithLabel("com.ddev.platform")
if err == nil {
for _, network := range removals {
Expand All @@ -60,7 +66,4 @@ func PowerOff() {
} else {
util.Warning("Unable to run dockerutil.FindNetworksWithLabel(): %v", err)
}

// Remove old network from DDEV before v1.22.4
dockerutil.RemoveNetworkWithWarningOnError(dockerutil.NetName)
}
37 changes: 35 additions & 2 deletions pkg/dockerutil/dockerutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type ComposeCmdOpts struct {

// EnsureNetwork will ensure the Docker network for DDEV is created.
func EnsureNetwork(client *docker.Client, name string) error {
// Pre-check for network duplicates
RemoveNetworkDuplicates(client, name)

if !NetExists(client, name) {
netOptions := docker.CreateNetworkOptions{
Name: name,
Expand All @@ -54,7 +57,6 @@ func EnsureNetwork(client *docker.Client, name string) error {
return err
}
output.UserOut.Println("Network", name, "created")

}
return nil
}
Expand Down Expand Up @@ -89,9 +91,18 @@ func NetworkExists(netName string) bool {
}

// RemoveNetwork removes the named Docker network
// netName can also be network's ID
func RemoveNetwork(netName string) error {
client := GetDockerClient()
err := client.RemoveNetwork(netName)
networks, _ := client.ListNetworks()
var err error
// loop through all networks because there may be duplicates
// and delete only by ID - it's unique, but the name isn't
for _, network := range networks {
if network.Name == netName || network.ID == netName {
err = client.RemoveNetwork(network.ID)
}
}
return err
}

Expand All @@ -107,6 +118,28 @@ func RemoveNetworkWithWarningOnError(netName string) {
}
}

// RemoveNetworkDuplicates removes the duplicates for the named Docker network
// This means that if there is only one network with this name - no action,
// and if there are several such networks, then we leave the first one, and delete the others
func RemoveNetworkDuplicates(client *docker.Client, netName string) {
networks, _ := client.ListNetworks()
networkMatchFound := false
for _, network := range networks {
if network.Name == netName || network.ID == netName {
if networkMatchFound == true {
err := client.RemoveNetwork(network.ID)
_, isNoSuchNetwork := err.(*docker.NoSuchNetwork)
// If it's a "no such network" there's no reason to report error
if err != nil && !isNoSuchNetwork {
util.Warning("Unable to remove network %s: %v", netName, err)
}
} else {
networkMatchFound = true
}
}
}
}

var DockerHost string
var DockerContext string

Expand Down