Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address race condition with docker network management
  • Loading branch information
matthewmcneely committed Oct 20, 2025
commit 4f422630a4d2448221f47d337adfe7ea3da76d28
35 changes: 29 additions & 6 deletions dgraphtest/local_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,30 @@ func (c *LocalCluster) init() error {

func (c *LocalCluster) createNetwork() error {
c.net.name = c.conf.prefix + "-net"

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()

// Check if network already exists
existingNet, err := c.dcli.NetworkInspect(ctx, c.net.name, network.InspectOptions{})
if err == nil {
// Network exists, reuse it
log.Printf("[INFO] reusing existing network %s (ID: %s)", c.net.name, existingNet.ID)
c.net.id = existingNet.ID
return nil
}

// Network doesn't exist, create it
opts := network.CreateOptions{
Driver: "bridge",
IPAM: &network.IPAM{Driver: "default"},
}

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
network, err := c.dcli.NetworkCreate(ctx, c.net.name, opts)
networkResp, err := c.dcli.NetworkCreate(ctx, c.net.name, opts)
if err != nil {
return errors.Wrap(err, "error creating network")
}
c.net.id = network.ID
c.net.id = networkResp.ID

return nil
}
Expand Down Expand Up @@ -256,6 +268,19 @@ func (c *LocalCluster) createContainer(dc dnode) (string, error) {
return "", err
}

// Verify the network still exists before creating container
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
if c.net.id != "" {
_, err := c.dcli.NetworkInspect(ctx, c.net.id, network.InspectOptions{})
if err != nil {
log.Printf("[WARNING] network %s (ID: %s) not found, recreating", c.net.name, c.net.id)
if err := c.createNetwork(); err != nil {
return "", errors.Wrap(err, "error recreating network")
}
}
}

cconf := &container.Config{Cmd: cmd, Image: image, WorkingDir: dc.workingDir(), ExposedPorts: dc.ports()}
hconf := &container.HostConfig{Mounts: mts, PublishAllPorts: true, PortBindings: dc.bindings(c.conf.portOffset)}
networkConfig := &network.NetworkingConfig{
Expand All @@ -267,8 +292,6 @@ func (c *LocalCluster) createContainer(dc dnode) (string, error) {
},
}

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
resp, err := c.dcli.ContainerCreate(ctx, cconf, hconf, networkConfig, nil, dc.cname())
if err != nil {
return "", errors.Wrapf(err, "error creating container %v", dc.cname())
Expand Down
Loading