api.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/compose-spec/compose-go/types"
  18. "github.com/docker/compose-cli/formatter"
  19. )
  20. const (
  21. // RestartPolicyNone Never restarts
  22. RestartPolicyNone = "none"
  23. // RestartPolicyAny Always restarts
  24. RestartPolicyAny = "any"
  25. // RestartPolicyOnFailure Restarts only on failure
  26. RestartPolicyOnFailure = "on-failure"
  27. // RestartPolicyRunNo Always restarts
  28. RestartPolicyRunNo = "no"
  29. // RestartPolicyRunAlways Always restarts
  30. RestartPolicyRunAlways = "always"
  31. )
  32. // RestartPolicyList all available restart policy values
  33. var RestartPolicyList = []string{RestartPolicyRunNo, RestartPolicyRunAlways, RestartPolicyOnFailure}
  34. // Container represents a created container
  35. type Container struct {
  36. ID string
  37. Status string
  38. Image string
  39. Command string
  40. CPUTime uint64
  41. MemoryUsage uint64
  42. PidsCurrent uint64
  43. PidsLimit uint64
  44. Config *RuntimeConfig `json:",omitempty"`
  45. HostConfig *HostConfig `json:",omitempty"`
  46. Ports []Port `json:",omitempty"`
  47. Platform string
  48. Healthcheck Healthcheck
  49. }
  50. // RuntimeConfig config of a created container
  51. type RuntimeConfig struct {
  52. Labels []string `json:",omitempty"`
  53. Env map[string]string `json:",omitempty"`
  54. // FQDN is the fqdn to use
  55. FQDN string `json:"fqdn,omitempty"`
  56. }
  57. // HostConfig config of the container host
  58. type HostConfig struct {
  59. RestartPolicy string
  60. CPUReservation float64
  61. CPULimit float64
  62. MemoryReservation uint64
  63. MemoryLimit uint64
  64. AutoRemove bool
  65. }
  66. // Port represents a published port of a container
  67. type Port struct {
  68. // HostPort is the port number on the host
  69. HostPort uint32
  70. // ContainerPort is the port number inside the container
  71. ContainerPort uint32
  72. // Protocol is the protocol of the port mapping
  73. Protocol string
  74. // HostIP is the host ip to use
  75. HostIP string
  76. }
  77. // ContainerConfig contains the configuration data about a container
  78. type ContainerConfig struct {
  79. // ID uniquely identifies the container
  80. ID string
  81. // Image specifies the image reference used for a container
  82. Image string
  83. // Command are the arguments passed to the container's entrypoint
  84. Command []string
  85. // Ports provide a list of published ports
  86. Ports []Port
  87. // Labels set labels to the container
  88. Labels map[string]string
  89. // Volumes to be mounted
  90. Volumes []string
  91. // Memlimit
  92. MemLimit formatter.MemBytes
  93. // CPUlimit
  94. CPULimit float64
  95. // Environment variables
  96. Environment []string
  97. // Restart policy condition
  98. RestartPolicyCondition string
  99. // DomainName Container NIS domain name
  100. DomainName string
  101. // AutoRemove sets the container to be removed automatically when stopped
  102. AutoRemove bool
  103. // Healthcheck contains the command and interval of the checks
  104. Healthcheck Healthcheck
  105. }
  106. // Healthcheck defines the configuration of a healthcheck
  107. type Healthcheck struct {
  108. // Disable disables the check
  109. Disable bool
  110. // Test is the command to be run to check the health of the container
  111. Test []string
  112. // Interval is the period in between the checks
  113. Interval types.Duration
  114. // Retries is the number of attempts before declaring the container as healthy or unhealthy
  115. Retries int
  116. // StartPeriod is the start delay before starting the checks
  117. StartPeriod types.Duration
  118. // Timeout is the timeout in between checks
  119. Timeout types.Duration
  120. }
  121. // ExecRequest contaiens configuration about an exec request
  122. type ExecRequest struct {
  123. Stdin io.Reader
  124. Stdout io.Writer
  125. Stderr io.Writer
  126. Command string
  127. Interactive bool
  128. Tty bool
  129. }
  130. // LogsRequest contains configuration about a log request
  131. type LogsRequest struct {
  132. Follow bool
  133. Tail string
  134. Width int
  135. Writer io.Writer
  136. }
  137. // DeleteRequest contains configuration about a delete request
  138. type DeleteRequest struct {
  139. Force bool
  140. }
  141. // Service interacts with the underlying container backend
  142. type Service interface {
  143. // List returns all the containers
  144. List(ctx context.Context, all bool) ([]Container, error)
  145. // Start starts a stopped container
  146. Start(ctx context.Context, containerID string) error
  147. // Stop stops the running container
  148. Stop(ctx context.Context, containerID string, timeout *uint32) error
  149. // Kill stops the running container
  150. Kill(ctx context.Context, containerID string, signal string) error
  151. // Run creates and starts a container
  152. Run(ctx context.Context, config ContainerConfig) error
  153. // Exec executes a command inside a running container
  154. Exec(ctx context.Context, containerName string, request ExecRequest) error
  155. // Logs returns all the logs of a container
  156. Logs(ctx context.Context, containerName string, request LogsRequest) error
  157. // Delete removes containers
  158. Delete(ctx context.Context, containerID string, request DeleteRequest) error
  159. // Inspect get a specific container
  160. Inspect(ctx context.Context, id string) (Container, error)
  161. }