api.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. CPULimit float64
  55. MemoryLimit uint64
  56. }
  57. // Port represents a published port of a container
  58. type Port struct {
  59. // HostPort is the port number on the host
  60. HostPort uint32
  61. // ContainerPort is the port number inside the container
  62. ContainerPort uint32
  63. // Protocol is the protocol of the port mapping
  64. Protocol string
  65. // HostIP is the host ip to use
  66. HostIP string
  67. }
  68. // ContainerConfig contains the configuration data about a container
  69. type ContainerConfig struct {
  70. // ID uniquely identifies the container
  71. ID string
  72. // Image specifies the iamge reference used for a container
  73. Image string
  74. // Command are the arguments passed to the container's entrypoint
  75. Command []string
  76. // Ports provide a list of published ports
  77. Ports []Port
  78. // Labels set labels to the container
  79. Labels map[string]string
  80. // Volumes to be mounted
  81. Volumes []string
  82. // Memlimit
  83. MemLimit formatter.MemBytes
  84. // CPUlimit
  85. CPULimit float64
  86. // Environment variables
  87. Environment []string
  88. // Restart policy condition
  89. RestartPolicyCondition string
  90. // DomainName Container NIS domain name
  91. DomainName string
  92. }
  93. // ExecRequest contaiens configuration about an exec request
  94. type ExecRequest struct {
  95. Stdin io.Reader
  96. Stdout io.Writer
  97. Stderr io.Writer
  98. Command string
  99. Interactive bool
  100. Tty bool
  101. }
  102. // LogsRequest contains configuration about a log request
  103. type LogsRequest struct {
  104. Follow bool
  105. Tail string
  106. Width int
  107. Writer io.Writer
  108. }
  109. // DeleteRequest contains configuration about a delete request
  110. type DeleteRequest struct {
  111. Force bool
  112. }
  113. // Service interacts with the underlying container backend
  114. type Service interface {
  115. // List returns all the containers
  116. List(ctx context.Context, all bool) ([]Container, error)
  117. // Start starts a stopped container
  118. Start(ctx context.Context, containerID string) error
  119. // Stop stops the running container
  120. Stop(ctx context.Context, containerID string, timeout *uint32) error
  121. // Kill stops the running container
  122. Kill(ctx context.Context, containerID string, signal string) error
  123. // Run creates and starts a container
  124. Run(ctx context.Context, config ContainerConfig) error
  125. // Exec executes a command inside a running container
  126. Exec(ctx context.Context, containerName string, request ExecRequest) error
  127. // Logs returns all the logs of a container
  128. Logs(ctx context.Context, containerName string, request LogsRequest) error
  129. // Delete removes containers
  130. Delete(ctx context.Context, containerID string, request DeleteRequest) error
  131. // Inspect get a specific container
  132. Inspect(ctx context.Context, id string) (Container, error)
  133. }