Skip to content

Commit

Permalink
fix: make project network external, fixes #5193 (#5305)
Browse files Browse the repository at this point in the history
Co-authored-by: Randy Fay <randy@randyfay.com>
  • Loading branch information
stasadev and rfay committed Oct 27, 2023
1 parent d518c1f commit 989c173
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
6 changes: 4 additions & 2 deletions pkg/ddevapp/app_compose_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,13 @@ networks:
ddev_default:
name: ddev_default
external: true
{{if .IsGitpod}}{{/* see https://github.com/ddev/ddev/issues/3766 */}}
default:
name: ${COMPOSE_PROJECT_NAME}_default
external: true
{{ if .IsGitpod }}{{/* see https://github.com/ddev/ddev/issues/3766 */}}
driver_opts:
com.docker.network.driver.mtu: 1440
{{end}}
{{ end }}

volumes:
{{if and (not .OmitDB) (ne .DBType "postgres") }}
Expand Down
7 changes: 7 additions & 0 deletions pkg/ddevapp/ddevapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2557,6 +2557,13 @@ 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
dockerutil.RemoveNetworkWithWarningOnError(os.Getenv("COMPOSE_PROJECT_NAME") + "_default")

return nil
}

Expand Down
20 changes: 15 additions & 5 deletions pkg/dockerutil/dockerutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,24 @@ func EnsureNetwork(client *docker.Client, name string) error {
return nil
}

// EnsureNetworkWithFatal creates or ensures the provided network exists or
// exits with fatal.
func EnsureNetworkWithFatal(name string) {
client := GetDockerClient()
err := EnsureNetwork(client, name)
if err != nil {
log.Fatalf("Failed to ensure Docker network %s: %v", name, err)
}
}

// EnsureDdevNetwork creates or ensures the DDEV network exists or
// exits with fatal.
func EnsureDdevNetwork() {
// Ensure we have the fallback global DDEV network
client := GetDockerClient()
err := EnsureNetwork(client, NetName)
if err != nil {
log.Fatalf("Failed to ensure Docker network %s: %v", NetName, err)
EnsureNetworkWithFatal(NetName)
// Ensure we have the current project network
if os.Getenv("COMPOSE_PROJECT_NAME") != "" {
EnsureNetworkWithFatal(os.Getenv("COMPOSE_PROJECT_NAME") + "_default")
}
}

Expand Down Expand Up @@ -607,7 +617,7 @@ func ComposeCmd(cmd *ComposeCmdOpts) (string, string, error) {
// Container (or Volume) ... Creating or Created or Stopping or Starting or Removing
// Container Stopped or Created
// No resource found to remove (when doing a stop and no project exists)
ignoreRegex := "(^ *(Network|Container|Volume) .* (Creat|Start|Stopp|Remov)ing$|^Container .*(Stopp|Creat)(ed|ing)$|Warning: No resource found to remove$|Pulling fs layer|Waiting|Downloading|Extracting|Verifying Checksum|Download complete|Pull complete)"
ignoreRegex := "(^ *(Network|Container|Volume) .* (Creat|Start|Stopp|Remov)ing$|^Container .*(Stopp|Creat)(ed|ing)$|Warning: No resource found to remove|Pulling fs layer|Waiting|Downloading|Extracting|Verifying Checksum|Download complete|Pull complete)"
downRE, err := regexp.Compile(ignoreRegex)
if err != nil {
util.Warning("Failed to compile regex %v: %v", ignoreRegex, err)
Expand Down

0 comments on commit 989c173

Please sign in to comment.