api.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // Container represents a created container
  33. type Container struct {
  34. ID string
  35. Status string
  36. Image string
  37. Command string
  38. CPUTime uint64
  39. MemoryUsage uint64
  40. PidsCurrent uint64
  41. PidsLimit uint64
  42. Config *RuntimeConfig `json:",omitempty"`
  43. HostConfig *HostConfig `json:",omitempty"`
  44. Ports []Port `json:",omitempty"`
  45. Platform string
  46. Healthcheck Healthcheck
  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. // Healthcheck contains the command and interval of the checks
  102. Healthcheck Healthcheck
  103. }
  104. // Healthcheck defines the configuration of a healthcheck
  105. type Healthcheck struct {
  106. // Disable disables the check
  107. Disable bool
  108. // Test is the command to be run to check the health of the container
  109. Test []string
  110. // Interval is the period in between the checks
  111. Interval types.Duration
  112. // Retries is the number of attempts before declaring the container as healthy or unhealthy
  113. Retries int
  114. // StartPeriod is the start delay before starting the checks
  115. StartPeriod types.Duration
  116. // Timeout is the timeout in between checks
  117. Timeout types.Duration
  118. }
  119. // ExecRequest contaiens configuration about an exec request
  120. type ExecRequest struct {
  121. Stdin io.Reader
  122. Stdout io.Writer
  123. Stderr io.Writer
  124. Command string
  125. Interactive bool
  126. Tty bool
  127. }
  128. // LogsRequest contains configuration about a log request
  129. type LogsRequest struct {
  130. Follow bool
  131. Tail string
  132. Width int
  133. Writer io.Writer
  134. }
  135. // DeleteRequest contains configuration about a delete request
  136. type DeleteRequest struct {
  137. Force bool
  138. }
  139. // Service interacts with the underlying container backend
  140. type Service interface {
  141. // List returns all the containers
  142. List(ctx context.Context, all bool) ([]Container, error)
  143. // Start starts a stopped container
  144. Start(ctx context.Context, containerID string) error
  145. // Stop stops the running container
  146. Stop(ctx context.Context, containerID string, timeout *uint32) error
  147. // Kill stops the running container
  148. Kill(ctx context.Context, containerID string, signal string) error
  149. // Run creates and starts a container
  150. Run(ctx context.Context, config ContainerConfig) error
  151. // Exec executes a command inside a running container
  152. Exec(ctx context.Context, containerName string, request ExecRequest) error
  153. // Logs returns all the logs of a container
  154. Logs(ctx context.Context, containerName string, request LogsRequest) error
  155. // Delete removes containers
  156. Delete(ctx context.Context, containerID string, request DeleteRequest) error
  157. // Inspect get a specific container
  158. Inspect(ctx context.Context, id string) (Container, error)
  159. }