code: func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
  	if endpointConfig == nil {
  		endpointConfig = &networktypes.EndpointSettings{}
  	}
  	container.Lock()
  	defer container.Unlock()
  
  	if !container.Running {
  		if container.RemovalInProgress || container.Dead {
  			return errRemovalContainer(container.ID)
  		}
  
  		n, err := daemon.FindNetwork(idOrName)
  		if err == nil && n != nil {
  			if err := daemon.updateNetworkConfig(container, n, endpointConfig, true); err != nil {
  				return err
  			}
  		} else {
  			container.NetworkSettings.Networks[idOrName] = &network.EndpointSettings{
  				EndpointSettings: endpointConfig,
  			}
  		}
  	} else {
  		if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
  			return err
  		}
  	}
  
  	return container.CheckpointTo(daemon.containersReplica)
  }
docs: // ConnectToNetwork connects a container to a network