inspect.go 3.7 KB

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