diff --git a/modules/compose/compose_api.go b/modules/compose/compose_api.go index 93bf220920..1299ff6a8f 100644 --- a/modules/compose/compose_api.go +++ b/modules/compose/compose_api.go @@ -193,8 +193,8 @@ type dockerCompose struct { // only one strategy can be added to a service, to use multiple use wait.ForAll(...) waitStrategies map[string]wait.Strategy - // used to synchronise writes to the containers map - containersLock sync.RWMutex + // Used to synchronise writes to the containers. + containersLock sync.Mutex // cache for containers that are part of the stack // used in ServiceContainer(...) function to avoid calls to the Docker API @@ -391,10 +391,6 @@ func (d *dockerCompose) Up(ctx context.Context, opts ...StackUpOption) (err erro termSignals = append(termSignals, termSignal) } - d.containersLock.Lock() - defer d.containersLock.Unlock() - d.containers[srv.Name] = dc - return nil }) } @@ -420,11 +416,6 @@ func (d *dockerCompose) Up(ctx context.Context, opts ...StackUpOption) (err erro return err } - // cache all the containers on compose.up - d.containersLock.Lock() - defer d.containersLock.Unlock() - d.containers[svc] = target - return strategy.WaitUntilReady(errGrpCtx, target) }) } @@ -511,6 +502,8 @@ func (d *dockerCompose) lookupContainer(ctx context.Context, svcName string) (*t ctr.SetProvider(dockerProvider) + d.containersLock.Lock() + defer d.containersLock.Unlock() d.containers[svcName] = ctr return ctr, nil @@ -520,9 +513,6 @@ func (d *dockerCompose) lookupContainer(ctx context.Context, svcName string) (*t // // Safe for concurrent calls. func (d *dockerCompose) lookupNetworks(ctx context.Context) error { - d.containersLock.Lock() - defer d.containersLock.Unlock() - networks, err := d.dockerClient.NetworkList(ctx, dockernetwork.ListOptions{ Filters: filters.NewArgs( filters.Arg("label", fmt.Sprintf("%s=%s", api.ProjectLabel, d.name)),