Repository: docker/compose
File: pkg/compose/create.go (Go)

Code before:
	if err != nil {
		s.events.On(errorEvent(networkEventName, err.Error()))
		return "", fmt.Errorf("failed to create network %s: %w", n.Name, err)
	}
	s.events.On(createdEvent(networkEventName))

	err = s.connectNetwork(ctx, n.Name, dangledContainers, nil)
	if err != nil {
		return "", err
	}

	// Emit container status events for containers that may have been restarted by the engine
	for _, c := range dangledContainers {
		inspected, err := s.apiClient().ContainerInspect(ctx, c.ID)
		if err != nil {
			// ignore inspect errors
			continue
		}
		name := "Container " + strings.TrimPrefix(inspected.Name, "/")
		if inspected.State != nil && inspected.State.Running {
			s.events.On(runningEvent(name))
		} else if inspected.State != nil && inspected.State.Status == container.StateCreated {
			s.events.On(createdEvent(name))
		} else if inspected.State != nil && inspected.State.Status == container.StateExited {
			s.events.On(stoppedEvent(name))
		}
	}

	return resp.ID, nil
}

func (s *composeService) removeDivergedNetwork(ctx context.Context, project *types.Project, name string, n *types.NetworkConfig) (Containers, error) {
	// Remove services attached to this network to force recreation
	var services []string
	for _, service := range project.Services.Filter(func(config types.ServiceConfig) bool {
		_, ok := config.Networks[name]
		return ok
	}) {
		services = append(services, service.Name)
	}

	// Stop containers so we can remove network
	// They will be restarted (actually: recreated) with the updated network
	err := s.stop(ctx, project.Name, api.StopOptions{
		Services: services,
		Project:  project,
	}, nil)
	if err != nil {
		return nil, err
	}


Review comment (suggestion):
The conditional logic is inconsistent: it checks `inspected.State.Running` (a boolean field) for running containers but checks `inspected.State.Status` (a string field) for created and exited containers. For consistency and correctness, all conditions should check the `Status` field. The first condition should be `inspected.State.Status == container.StateRunning` to match the pattern used in similar code elsewhere in the codebase (e.g., ps.go lines 72-77).

Code after:
	if err != nil {
		s.events.On(errorEvent(networkEventName, err.Error()))
		return "", fmt.Errorf("failed to create network %s: %w", n.Name, err)
	}
	s.events.On(createdEvent(networkEventName))

	err = s.connectNetwork(ctx, n.Name, dangledContainers, nil)
	if err != nil {
		return "", err
	}

	return resp.ID, nil
}

func (s *composeService) removeDivergedNetwork(ctx context.Context, project *types.Project, name string, n *types.NetworkConfig) (Containers, error) {
	// Remove services attached to this network to force recreation
	var services []string
	for _, service := range project.Services.Filter(func(config types.ServiceConfig) bool {
		_, ok := config.Networks[name]
		return ok
	}) {
		services = append(services, service.Name)
	}

	// Stop containers so we can remove network
	// They will be restarted (actually: recreated) with the updated network
	err := s.stop(ctx, project.Name, api.StopOptions{
		Services: services,
		Project:  project,
	}, nil)
	if err != nil {
		return nil, err
	}

	containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true, services...)
	if err != nil {
		return nil, err
	}

	err = s.disconnectNetwork(ctx, n.Name, containers)
	if err != nil {
		return nil, err
	}

	err = s.apiClient().NetworkRemove(ctx, n.Name)
	eventName := fmt.Sprintf("Network %s", n.Name)
	s.events.On(removedEvent(eventName))
	return containers, err
}

func (s *composeService) disconnectNetwork(