exec.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. var env []string
  43. for k, v := range service.Environment.OverrideBy(types.NewMappingWithEquals(opts.Environment)).
  44. Resolve(func(s string) (string, bool) {
  45. v, ok := project.Environment[s]
  46. return v, ok
  47. }).
  48. RemoveEmpty() {
  49. env = append(env, fmt.Sprintf("%s=%s", k, *v))
  50. }
  51. exec, err := s.apiClient.ContainerExecCreate(ctx, container.ID, apitypes.ExecConfig{
  52. Cmd: opts.Command,
  53. Env: env,
  54. User: opts.User,
  55. Privileged: opts.Privileged,
  56. Tty: opts.Tty,
  57. Detach: opts.Detach,
  58. WorkingDir: opts.WorkingDir,
  59. AttachStdin: true,
  60. AttachStdout: true,
  61. AttachStderr: true,
  62. })
  63. if err != nil {
  64. return 0, err
  65. }
  66. if opts.Detach {
  67. return 0, s.apiClient.ContainerExecStart(ctx, exec.ID, apitypes.ExecStartCheck{
  68. Detach: true,
  69. Tty: opts.Tty,
  70. })
  71. }
  72. resp, err := s.apiClient.ContainerExecAttach(ctx, exec.ID, apitypes.ExecStartCheck{
  73. Detach: false,
  74. Tty: opts.Tty,
  75. })
  76. if err != nil {
  77. return 0, err
  78. }
  79. defer resp.Close()
  80. if opts.Tty {
  81. s.monitorTTySize(ctx, exec.ID, s.apiClient.ContainerExecResize)
  82. if err != nil {
  83. return 0, err
  84. }
  85. }
  86. readChannel := make(chan error)
  87. writeChannel := make(chan error)
  88. go func() {
  89. _, err := io.Copy(opts.Writer, resp.Reader)
  90. readChannel <- err
  91. }()
  92. go func() {
  93. _, err := io.Copy(resp.Conn, opts.Reader)
  94. writeChannel <- err
  95. }()
  96. select {
  97. case err = <-readChannel:
  98. break
  99. case err = <-writeChannel:
  100. break
  101. }
  102. if err != nil {
  103. return 0, err
  104. }
  105. return s.getExecExitStatus(ctx, exec.ID)
  106. }
  107. func (s *composeService) getExecExitStatus(ctx context.Context, execID string) (int, error) {
  108. resp, err := s.apiClient.ContainerExecInspect(ctx, execID)
  109. if err != nil {
  110. return 0, err
  111. }
  112. return resp.ExitCode, nil
  113. }