attach.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 compose
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "io"
  19. "strings"
  20. "github.com/compose-spec/compose-go/v2/types"
  21. containerType "github.com/docker/docker/api/types/container"
  22. "github.com/docker/docker/pkg/stdcopy"
  23. "github.com/sirupsen/logrus"
  24. "github.com/docker/compose/v5/pkg/api"
  25. "github.com/docker/compose/v5/pkg/utils"
  26. )
  27. func (s *composeService) attach(ctx context.Context, project *types.Project, listener api.ContainerEventListener, selectedServices []string) (Containers, error) {
  28. containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true, selectedServices...)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if len(containers) == 0 {
  33. return containers, nil
  34. }
  35. containers.sorted() // This enforces predictable colors assignment
  36. var names []string
  37. for _, c := range containers {
  38. names = append(names, getContainerNameWithoutProject(c))
  39. }
  40. _, err = fmt.Fprintf(s.stdout(), "Attaching to %s\n", strings.Join(names, ", "))
  41. if err != nil {
  42. logrus.Debugf("failed to write attach message: %v", err)
  43. }
  44. for _, ctr := range containers {
  45. err := s.attachContainer(ctx, ctr, listener)
  46. if err != nil {
  47. return nil, err
  48. }
  49. }
  50. return containers, nil
  51. }
  52. func (s *composeService) attachContainer(ctx context.Context, container containerType.Summary, listener api.ContainerEventListener) error {
  53. service := container.Labels[api.ServiceLabel]
  54. name := getContainerNameWithoutProject(container)
  55. return s.doAttachContainer(ctx, service, container.ID, name, listener)
  56. }
  57. func (s *composeService) doAttachContainer(ctx context.Context, service, id, name string, listener api.ContainerEventListener) error {
  58. inspect, err := s.apiClient().ContainerInspect(ctx, id)
  59. if err != nil {
  60. return err
  61. }
  62. wOut := utils.GetWriter(func(line string) {
  63. listener(api.ContainerEvent{
  64. Type: api.ContainerEventLog,
  65. Source: name,
  66. ID: id,
  67. Service: service,
  68. Line: line,
  69. })
  70. })
  71. wErr := utils.GetWriter(func(line string) {
  72. listener(api.ContainerEvent{
  73. Type: api.ContainerEventErr,
  74. Source: name,
  75. ID: id,
  76. Service: service,
  77. Line: line,
  78. })
  79. })
  80. err = s.attachContainerStreams(ctx, id, inspect.Config.Tty, wOut, wErr)
  81. if err != nil {
  82. return err
  83. }
  84. return nil
  85. }
  86. func (s *composeService) attachContainerStreams(ctx context.Context, container string, tty bool, stdout, stderr io.WriteCloser) error {
  87. streamOut, err := s.getContainerStreams(ctx, container)
  88. if err != nil {
  89. return err
  90. }
  91. if stdout != nil {
  92. go func() {
  93. defer func() {
  94. if err := stdout.Close(); err != nil {
  95. logrus.Debugf("failed to close stdout: %v", err)
  96. }
  97. if err := stderr.Close(); err != nil {
  98. logrus.Debugf("failed to close stderr: %v", err)
  99. }
  100. if err := streamOut.Close(); err != nil {
  101. logrus.Debugf("failed to close stream output: %v", err)
  102. }
  103. }()
  104. var err error
  105. if tty {
  106. _, err = io.Copy(stdout, streamOut)
  107. } else {
  108. _, err = stdcopy.StdCopy(stdout, stderr, streamOut)
  109. }
  110. if err != nil && !errors.Is(err, io.EOF) {
  111. logrus.Debugf("stream copy error for container %s: %v", container, err)
  112. }
  113. }()
  114. }
  115. return nil
  116. }
  117. func (s *composeService) getContainerStreams(ctx context.Context, container string) (io.ReadCloser, error) {
  118. cnx, err := s.apiClient().ContainerAttach(ctx, container, containerType.AttachOptions{
  119. Stream: true,
  120. Stdin: false,
  121. Stdout: true,
  122. Stderr: true,
  123. Logs: false,
  124. })
  125. if err == nil {
  126. stdout := ContainerStdout{HijackedResponse: cnx}
  127. return stdout, nil
  128. }
  129. // Fallback to logs API
  130. logs, err := s.apiClient().ContainerLogs(ctx, container, containerType.LogsOptions{
  131. ShowStdout: true,
  132. ShowStderr: true,
  133. Follow: true,
  134. })
  135. if err != nil {
  136. return nil, err
  137. }
  138. return logs, nil
  139. }