api.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package containers
  14. import (
  15. "context"
  16. "io"
  17. "github.com/docker/compose-cli/formatter"
  18. )
  19. const (
  20. // RestartPolicyNone Never restarts
  21. RestartPolicyNone = "none"
  22. // RestartPolicyAny Always restarts
  23. RestartPolicyAny = "any"
  24. // RestartPolicyOnFailure Restarts only on failure
  25. RestartPolicyOnFailure = "on-failure"
  26. // RestartPolicyRunNo Always restarts
  27. RestartPolicyRunNo = "no"
  28. // RestartPolicyRunAlways Always restarts
  29. RestartPolicyRunAlways = "always"
  30. )
  31. // RestartPolicyList all available restart policy values
  32. var RestartPolicyList = []string{RestartPolicyRunNo, RestartPolicyRunAlways, RestartPolicyOnFailure}
  33. // Container represents a created container
  34. type Container struct {
  35. ID string
  36. Status string
  37. Image string
  38. Command string
  39. CPUTime uint64
  40. MemoryUsage uint64
  41. PidsCurrent uint64
  42. PidsLimit uint64
  43. Config *RuntimeConfig `json:",omitempty"`
  44. HostConfig *HostConfig `json:",omitempty"`
  45. Ports []Port `json:",omitempty"`
  46. Platform string
  47. }
  48. // RuntimeConfig config of a created container
  49. type RuntimeConfig struct {
  50. Labels []string `json:",omitempty"`
  51. Env map[string]string `json:",omitempty"`
  52. // FQDN is the fqdn to use
  53. FQDN string `json:"fqdn,omitempty"`
  54. }
  55. // HostConfig config of the container host
  56. type HostConfig struct {
  57. RestartPolicy string
  58. CPUReservation float64
  59. CPULimit float64
  60. MemoryReservation uint64
  61. MemoryLimit uint64
  62. AutoRemove bool
  63. }
  64. // Port represents a published port of a container
  65. type Port struct {
  66. // HostPort is the port number on the host
  67. HostPort uint32
  68. // ContainerPort is the port number inside the container
  69. ContainerPort uint32
  70. // Protocol is the protocol of the port mapping
  71. Protocol string
  72. // HostIP is the host ip to use
  73. HostIP string
  74. }
  75. // ContainerConfig contains the configuration data about a container
  76. type ContainerConfig struct {
  77. // ID uniquely identifies the container
  78. ID string
  79. // Image specifies the image reference used for a container
  80. Image string
  81. // Command are the arguments passed to the container's entrypoint
  82. Command []string
  83. // Ports provide a list of published ports
  84. Ports []Port
  85. // Labels set labels to the container
  86. Labels map[string]string
  87. // Volumes to be mounted
  88. Volumes []string
  89. // Memlimit
  90. MemLimit formatter.MemBytes
  91. // CPUlimit
  92. CPULimit float64
  93. // Environment variables
  94. Environment []string
  95. // Restart policy condition
  96. RestartPolicyCondition string
  97. // DomainName Container NIS domain name
  98. DomainName string
  99. // AutoRemove sets the container to be removed automatically when stopped
  100. AutoRemove bool
  101. }
  102. // ExecRequest contaiens configuration about an exec request
  103. type ExecRequest struct {
  104. Stdin io.Reader
  105. Stdout io.Writer
  106. Stderr io.Writer
  107. Command string
  108. Interactive bool
  109. Tty bool
  110. }
  111. // LogsRequest contains configuration about a log request
  112. type LogsRequest struct {
  113. Follow bool
  114. Tail string
  115. Width int
  116. Writer io.Writer
  117. }
  118. // DeleteRequest contains configuration about a delete request
  119. type DeleteRequest struct {
  120. Force bool
  121. }
  122. // Service interacts with the underlying container backend
  123. type Service interface {
  124. // List returns all the containers
  125. List(ctx context.Context, all bool) ([]Container, error)
  126. // Start starts a stopped container
  127. Start(ctx context.Context, containerID string) error
  128. // Stop stops the running container
  129. Stop(ctx context.Context, containerID string, timeout *uint32) error
  130. // Kill stops the running container
  131. Kill(ctx context.Context, containerID string, signal string) error
  132. // Run creates and starts a container
  133. Run(ctx context.Context, config ContainerConfig) error
  134. // Exec executes a command inside a running container
  135. Exec(ctx context.Context, containerName string, request ExecRequest) error
  136. // Logs returns all the logs of a container
  137. Logs(ctx context.Context, containerName string, request LogsRequest) error
  138. // Delete removes containers
  139. Delete(ctx context.Context, containerID string, request DeleteRequest) error
  140. // Inspect get a specific container
  141. Inspect(ctx context.Context, id string) (Container, error)
  142. }