exec.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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) error {
  24. service, err := project.GetService(opts.Service)
  25. if err != nil {
  26. return 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 err
  37. }
  38. if len(containers) < 1 {
  39. return 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 err
  56. }
  57. if opts.Detach {
  58. return 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 err
  69. }
  70. defer resp.Close()
  71. readChannel := make(chan error, 10)
  72. writeChannel := make(chan error, 10)
  73. go func() {
  74. _, err := io.Copy(opts.Writer, resp.Reader)
  75. readChannel <- err
  76. }()
  77. go func() {
  78. _, err := io.Copy(resp.Conn, opts.Reader)
  79. writeChannel <- err
  80. }()
  81. for {
  82. select {
  83. case err := <-readChannel:
  84. return err
  85. case err := <-writeChannel:
  86. return err
  87. }
  88. }
  89. }