api.go 4.2 KB

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