MCPcopy Index your code
hub / github.com/moby/moby / initializeNetworking

Method initializeNetworking

daemon/container_operations.go:450–529  ·  view source on GitHub ↗

initializeNetworking prepares network configuration for a new container. If it creates a new libnetwork.Sandbox it's returned as newSandbox, for the caller to Delete() if the container setup fails later in the process.

(ctx context.Context, cfg *config.Config, ctr *container.Container)

Source from the content-addressed store, hash-verified

448// If it creates a new libnetwork.Sandbox it's returned as newSandbox, for
449// the caller to Delete() if the container setup fails later in the process.
450func (daemon *Daemon) initializeNetworking(ctx context.Context, cfg *config.Config, ctr *container.Container) (newSandbox *libnetwork.Sandbox, retErr error) {
451 if daemon.netController == nil || ctr.Config.NetworkDisabled {
452 return nil, nil
453 }
454
455 // Cleanup any stale sandbox left over due to ungraceful daemon shutdown
456 if err := daemon.runInNetNS(func() error {
457 return daemon.netController.SandboxDestroy(ctx, ctr.ID)
458 }); err != nil {
459 log.G(ctx).WithError(err).Errorf("failed to cleanup up stale network sandbox for container %s", ctr.ID)
460 }
461
462 if ctr.HostConfig.NetworkMode.IsContainer() {
463 // we need to get the hosts files from the container to join
464 nc, err := daemon.getNetworkedContainer(ctr.ID, ctr.HostConfig.NetworkMode.ConnectedContainer())
465 if err != nil {
466 return nil, err
467 }
468
469 err = daemon.initializeNetworkingPaths(ctr, nc)
470 if err != nil {
471 return nil, err
472 }
473
474 ctr.Config.Hostname = nc.Config.Hostname
475 ctr.Config.Domainname = nc.Config.Domainname
476 return nil, nil
477 }
478
479 if ctr.HostConfig.NetworkMode.IsHost() && ctr.Config.Hostname == "" {
480 hn, err := os.Hostname()
481 if err != nil {
482 return nil, err
483 }
484 ctr.Config.Hostname = hn
485 }
486
487 daemon.updateContainerNetworkSettings(ctr, nil)
488
489 sbOptions, err := buildSandboxOptions(cfg, ctr)
490 if err != nil {
491 return nil, err
492 }
493 sb, err := daemon.netController.NewSandbox(ctx, ctr.ID, sbOptions...)
494 if err != nil {
495 return nil, err
496 }
497
498 setNetworkSandbox(ctr, sb)
499
500 defer func() {
501 if retErr != nil {
502 if err := daemon.runInNetNS(func() error {
503 return sb.Delete(ctx)
504 }); err != nil {
505 log.G(ctx).WithFields(log.Fields{
506 "error": err,
507 "container": ctr.ID,

Callers 1

containerStartMethod · 0.95

Calls 15

runInNetNSMethod · 0.95
getNetworkedContainerMethod · 0.95
DeleteMethod · 0.95
allocateNetworkMethod · 0.95
buildSandboxOptionsFunction · 0.85
setNetworkSandboxFunction · 0.85
SandboxDestroyMethod · 0.80
ErrorfMethod · 0.80
ConnectedContainerMethod · 0.80
HostnameMethod · 0.80

Tested by

no test coverage detected