@@ -250,6 +250,12 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
250250 return nil , errors .Wrap (errtypes .ErrAlreadyExisted , "container name: " + name )
251251 }
252252
253+ // set hostname.
254+ if config .Hostname .String () == "" {
255+ // if hostname is empty, take the part of id as the hostname
256+ config .Hostname = strfmt .Hostname (id [:12 ])
257+ }
258+
253259 // set container runtime
254260 if config .HostConfig .Runtime == "" {
255261 config .HostConfig .Runtime = mgr .Config .DefaultRuntime
@@ -322,13 +328,12 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
322328 networkMode := config .HostConfig .NetworkMode
323329 if networkMode == "" {
324330 config .HostConfig .NetworkMode = "bridge"
325- container .Config .NetworkDisabled = true
326331 }
327332 container .NetworkSettings = new (types.NetworkSettings )
328333 if len (config .NetworkingConfig .EndpointsConfig ) > 0 {
329334 container .NetworkSettings .Networks = config .NetworkingConfig .EndpointsConfig
330335 }
331- if container .NetworkSettings .Networks == nil && networkMode != "" && ! IsContainer (networkMode ) {
336+ if container .NetworkSettings .Networks == nil && ! IsContainer (config . HostConfig . NetworkMode ) {
332337 container .NetworkSettings .Networks = make (map [string ]* types.EndpointSettings )
333338 container .NetworkSettings .Networks [config .HostConfig .NetworkMode ] = new (types.EndpointSettings )
334339 }
@@ -454,33 +459,53 @@ func (mgr *ContainerManager) start(ctx context.Context, c *Container, detachKeys
454459 c .ResolvConfPath = origContainer .ResolvConfPath
455460 c .Config .Hostname = origContainer .Config .Hostname
456461 c .Config .Domainname = origContainer .Config .Domainname
457- }
462+ } else {
463+ // initialise host network mode
464+ if IsHost (networkMode ) {
465+ hostname , err := os .Hostname ()
466+ if err != nil {
467+ return err
468+ }
469+ c .Config .Hostname = strfmt .Hostname (hostname )
470+ }
458471
459- // initialise host network mode
460- if IsHost (networkMode ) {
461- hostname , err := os .Hostname ()
462- if err != nil {
472+ // build the network related path.
473+ if err := mgr .buildNetworkRelatedPath (c ); err != nil {
463474 return err
464475 }
465- c .Config .Hostname = strfmt .Hostname (hostname )
466- }
467476
468- // initialise network endpoint
469- if c .NetworkSettings != nil {
470- for name , endpointSetting := range c .NetworkSettings .Networks {
471- endpoint := mgr .buildContainerEndpoint (c )
472- endpoint .Name = name
473- endpoint .EndpointConfig = endpointSetting
474- if _ , err := mgr .NetworkMgr .EndpointCreate (ctx , endpoint ); err != nil {
475- logrus .Errorf ("failed to create endpoint: %v" , err )
476- return err
477+ // initialise network endpoint
478+ if c .NetworkSettings != nil {
479+ for name , endpointSetting := range c .NetworkSettings .Networks {
480+ endpoint := mgr .buildContainerEndpoint (c )
481+ endpoint .Name = name
482+ endpoint .EndpointConfig = endpointSetting
483+ if _ , err := mgr .NetworkMgr .EndpointCreate (ctx , endpoint ); err != nil {
484+ logrus .Errorf ("failed to create endpoint: %v" , err )
485+ return err
486+ }
477487 }
478488 }
479489 }
480490
481491 return mgr .createContainerdContainer (ctx , c )
482492}
483493
494+ // buildNetworkRelatedPath builds the network related path.
495+ func (mgr * ContainerManager ) buildNetworkRelatedPath (c * Container ) error {
496+ // set the hosts file path.
497+ c .HostsPath = path .Join (mgr .Store .Path (c .ID ), "hosts" )
498+
499+ // set the resolv.conf file path.
500+ c .ResolvConfPath = path .Join (mgr .Store .Path (c .ID ), "resolv.conf" )
501+
502+ // set the hostname file path.
503+ c .HostnamePath = path .Join (mgr .Store .Path (c .ID ), "hostname" )
504+
505+ // write the hostname file, other files are filled by libnetwork.
506+ return ioutil .WriteFile (c .HostnamePath , []byte (c .Config .Hostname + "\n " ), 0644 )
507+ }
508+
484509func (mgr * ContainerManager ) createContainerdContainer (ctx context.Context , c * Container ) error {
485510 // CgroupParent from HostConfig will be first priority to use,
486511 // then will be value from mgr.Config.CgroupParent
0 commit comments