exec.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "fmt"
  17. "io"
  18. "github.com/compose-spec/compose-go/types"
  19. apitypes "github.com/docker/docker/api/types"
  20. "github.com/docker/docker/api/types/filters"
  21. "github.com/docker/compose-cli/api/compose"
  22. )
  23. func (s *composeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
  24. service, err := project.GetService(opts.Service)
  25. if err != nil {
  26. return 0, err
  27. }
  28. containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{
  29. Filters: filters.NewArgs(
  30. projectFilter(project.Name),
  31. serviceFilter(service.Name),
  32. filters.Arg("label", fmt.Sprintf("%s=%d", containerNumberLabel, opts.Index)),
  33. ),
  34. })
  35. if err != nil {
  36. return 0, err
  37. }
  38. if len(containers) < 1 {
  39. return 0, fmt.Errorf("container %s not running", getContainerName(project.Name, service, opts.Index))
  40. }
  41. container := containers[0]
  42. exec, err := s.apiClient.ContainerExecCreate(ctx, container.ID, apitypes.ExecConfig{
  43. Cmd: opts.Command,
  44. Env: opts.Environment,
  45. User: opts.User,
  46. Privileged: opts.Privileged,
  47. Tty: opts.Tty,
  48. Detach: opts.Detach,
  49. WorkingDir: opts.WorkingDir,
  50. AttachStdin: true,
  51. AttachStdout: true,
  52. AttachStderr: true,
  53. })
  54. if err != nil {
  55. return 0, err
  56. }
  57. if opts.Detach {
  58. return 0, s.apiClient.ContainerExecStart(ctx, exec.ID, apitypes.ExecStartCheck{
  59. Detach: true,
  60. Tty: opts.Tty,
  61. })
  62. }
  63. resp, err := s.apiClient.ContainerExecAttach(ctx, exec.ID, apitypes.ExecStartCheck{
  64. Detach: false,
  65. Tty: opts.Tty,
  66. })
  67. if err != nil {
  68. return 0, err
  69. }
  70. defer resp.Close()
  71. if opts.Tty {
  72. s.monitorTTySize(ctx, exec.ID, s.apiClient.ContainerExecResize)
  73. if err != nil {
  74. return 0, err
  75. }
  76. }
  77. readChannel := make(chan error)
  78. writeChannel := make(chan error)
  79. go func() {
  80. _, err := io.Copy(opts.Writer, resp.Reader)
  81. readChannel <- err
  82. }()
  83. go func() {
  84. _, err := io.Copy(resp.Conn, opts.Reader)
  85. writeChannel <- err
  86. }()
  87. select {
  88. case err = <-readChannel:
  89. break
  90. case err = <-writeChannel:
  91. break
  92. }
  93. if err != nil {
  94. return 0, err
  95. }
  96. return s.getExecExitStatus(ctx, exec.ID)
  97. }
  98. func (s *composeService) getExecExitStatus(ctx context.Context, execID string) (int, error) {
  99. resp, err := s.apiClient.ContainerExecInspect(ctx, execID)
  100. if err != nil {
  101. return 0, err
  102. }
  103. return resp.ExitCode, nil
  104. }