github.com/slava-ustovytski/docker@v1.8.2-rc1/daemon/inspect.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/docker/docker/api/types"
     8  )
     9  
    10  func (daemon *Daemon) ContainerInspect(name string) (*types.ContainerJSON, error) {
    11  	container, err := daemon.Get(name)
    12  	if err != nil {
    13  		return nil, err
    14  	}
    15  
    16  	container.Lock()
    17  	defer container.Unlock()
    18  
    19  	base, err := daemon.getInspectData(container)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	mountPoints := make([]types.MountPoint, 0, len(container.MountPoints))
    25  	for _, m := range container.MountPoints {
    26  		mountPoints = append(mountPoints, types.MountPoint{
    27  			Name:        m.Name,
    28  			Source:      m.Path(),
    29  			Destination: m.Destination,
    30  			Driver:      m.Driver,
    31  			Mode:        m.Relabel,
    32  			RW:          m.RW,
    33  		})
    34  	}
    35  
    36  	return &types.ContainerJSON{base, mountPoints, container.Config}, nil
    37  }
    38  
    39  func (daemon *Daemon) ContainerInspectPre120(name string) (*types.ContainerJSONPre120, error) {
    40  	container, err := daemon.Get(name)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	container.Lock()
    46  	defer container.Unlock()
    47  
    48  	base, err := daemon.getInspectData(container)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	volumes := make(map[string]string)
    54  	volumesRW := make(map[string]bool)
    55  	for _, m := range container.MountPoints {
    56  		volumes[m.Destination] = m.Path()
    57  		volumesRW[m.Destination] = m.RW
    58  	}
    59  
    60  	config := &types.ContainerConfig{
    61  		container.Config,
    62  		container.hostConfig.Memory,
    63  		container.hostConfig.MemorySwap,
    64  		container.hostConfig.CpuShares,
    65  		container.hostConfig.CpusetCpus,
    66  	}
    67  
    68  	return &types.ContainerJSONPre120{base, volumes, volumesRW, config}, nil
    69  }
    70  
    71  func (daemon *Daemon) getInspectData(container *Container) (*types.ContainerJSONBase, error) {
    72  	// make a copy to play with
    73  	hostConfig := *container.hostConfig
    74  
    75  	if children, err := daemon.Children(container.Name); err == nil {
    76  		for linkAlias, child := range children {
    77  			hostConfig.Links = append(hostConfig.Links, fmt.Sprintf("%s:%s", child.Name, linkAlias))
    78  		}
    79  	}
    80  	// we need this trick to preserve empty log driver, so
    81  	// container will use daemon defaults even if daemon change them
    82  	if hostConfig.LogConfig.Type == "" {
    83  		hostConfig.LogConfig = daemon.defaultLogConfig
    84  	}
    85  
    86  	containerState := &types.ContainerState{
    87  		Running:    container.State.Running,
    88  		Paused:     container.State.Paused,
    89  		Restarting: container.State.Restarting,
    90  		OOMKilled:  container.State.OOMKilled,
    91  		Dead:       container.State.Dead,
    92  		Pid:        container.State.Pid,
    93  		ExitCode:   container.State.ExitCode,
    94  		Error:      container.State.Error,
    95  		StartedAt:  container.State.StartedAt.Format(time.RFC3339Nano),
    96  		FinishedAt: container.State.FinishedAt.Format(time.RFC3339Nano),
    97  	}
    98  
    99  	contJSONBase := &types.ContainerJSONBase{
   100  		Id:              container.ID,
   101  		Created:         container.Created.Format(time.RFC3339Nano),
   102  		Path:            container.Path,
   103  		Args:            container.Args,
   104  		State:           containerState,
   105  		Image:           container.ImageID,
   106  		NetworkSettings: container.NetworkSettings,
   107  		ResolvConfPath:  container.ResolvConfPath,
   108  		HostnamePath:    container.HostnamePath,
   109  		HostsPath:       container.HostsPath,
   110  		LogPath:         container.LogPath,
   111  		Name:            container.Name,
   112  		RestartCount:    container.RestartCount,
   113  		Driver:          container.Driver,
   114  		ExecDriver:      container.ExecDriver,
   115  		MountLabel:      container.MountLabel,
   116  		ProcessLabel:    container.ProcessLabel,
   117  		AppArmorProfile: container.AppArmorProfile,
   118  		ExecIDs:         container.GetExecIDs(),
   119  		HostConfig:      &hostConfig,
   120  	}
   121  
   122  	contJSONBase.GraphDriver.Name = container.Driver
   123  	graphDriverData, err := daemon.driver.GetMetadata(container.ID)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	contJSONBase.GraphDriver.Data = graphDriverData
   128  
   129  	return contJSONBase, nil
   130  }
   131  
   132  func (daemon *Daemon) ContainerExecInspect(id string) (*execConfig, error) {
   133  	eConfig, err := daemon.getExecConfig(id)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	return eConfig, nil
   138  }