run.go 3.8 KB

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