exec.go 3.2 KB

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