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