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