api.go 4.5 KB

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