run.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "os"
  19. "github.com/compose-spec/compose-go/types"
  20. "github.com/docker/compose-cli/api/compose"
  21. convert "github.com/docker/compose-cli/local/moby"
  22. apitypes "github.com/docker/docker/api/types"
  23. moby "github.com/docker/docker/pkg/stringid"
  24. )
  25. func (s *composeService) CreateOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
  26. service, err := project.GetService(opts.Name)
  27. if err != nil {
  28. return "", err
  29. }
  30. if err = s.ensureImagesExists(ctx, project); err != nil {
  31. return "", err
  32. }
  33. if err := s.ensureProjectNetworks(ctx, project); err != nil {
  34. return "", err
  35. }
  36. if err := s.ensureProjectVolumes(ctx, project); err != nil {
  37. return "", err
  38. }
  39. if err = s.ensureRequiredServices(ctx, project, service); err != nil {
  40. return "", err
  41. }
  42. updateOneOffServiceConfig(&service, project.Name, opts)
  43. err = s.createContainer(ctx, project, service, service.ContainerName, 1)
  44. if err != nil {
  45. return "", err
  46. }
  47. return service.ContainerName, err
  48. }
  49. func (s *composeService) Run(ctx context.Context, container string, detach bool) error {
  50. if detach {
  51. return s.apiClient.ContainerStart(ctx, container, apitypes.ContainerStartOptions{})
  52. }
  53. cnx, err := s.apiClient.ContainerAttach(ctx, container, apitypes.ContainerAttachOptions{
  54. Stream: true,
  55. Stdin: true,
  56. Stdout: true,
  57. Stderr: true,
  58. Logs: true,
  59. })
  60. if err != nil {
  61. return err
  62. }
  63. defer cnx.Close()
  64. stdout := convert.ContainerStdout{HijackedResponse: cnx}
  65. stdin := convert.ContainerStdin{HijackedResponse: cnx}
  66. readChannel := make(chan error, 10)
  67. writeChannel := make(chan error, 10)
  68. go func() {
  69. _, err := io.Copy(os.Stdout, cnx.Reader)
  70. readChannel <- err
  71. }()
  72. go func() {
  73. _, err := io.Copy(stdin, os.Stdin)
  74. writeChannel <- err
  75. }()
  76. go func() {
  77. <-ctx.Done()
  78. stdout.Close() //nolint:errcheck
  79. stdin.Close() //nolint:errcheck
  80. }()
  81. // start container
  82. err = s.apiClient.ContainerStart(ctx, container, apitypes.ContainerStartOptions{})
  83. if err != nil {
  84. return err
  85. }
  86. for {
  87. select {
  88. case err := <-readChannel:
  89. return err
  90. case err := <-writeChannel:
  91. return err
  92. }
  93. }
  94. }
  95. func updateOneOffServiceConfig(service *types.ServiceConfig, projectName string, opts compose.RunOptions) {
  96. if len(opts.Command) > 0 {
  97. service.Command = opts.Command
  98. }
  99. slug := moby.GenerateRandomID()
  100. service.Scale = 1
  101. service.ContainerName = fmt.Sprintf("%s_%s_run_%s", projectName, service.Name, moby.TruncateID(slug))
  102. service.Labels = types.Labels{
  103. slugLabel: slug,
  104. oneoffLabel: "True",
  105. }
  106. service.Tty = true
  107. service.StdinOpen = true
  108. }
  109. func (s *composeService) ensureRequiredServices(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
  110. err := InDependencyOrder(ctx, project, func(c context.Context, svc types.ServiceConfig) error {
  111. if svc.Name != service.Name { // only start dependencies, not service to run one-off
  112. return s.ensureService(c, project, svc)
  113. }
  114. return nil
  115. })
  116. if err != nil {
  117. return err
  118. }
  119. return s.Start(ctx, project, nil)
  120. }