github.com/jandre/docker@v1.7.0/daemon/inspect.go (about)

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