run.go 3.8 KB

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