run.go 3.4 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. "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. originalServices := project.Services
  27. dependencies := []types.ServiceConfig{}
  28. var requestedService types.ServiceConfig
  29. for _, service := range originalServices {
  30. if service.Name != opts.Name {
  31. dependencies = append(dependencies, service)
  32. } else {
  33. requestedService = service
  34. }
  35. }
  36. project.Services = types.Services(dependencies)
  37. if err := s.Create(ctx, project); err != nil {
  38. return "", err
  39. }
  40. if err := s.Start(ctx, project, nil); err != nil {
  41. return "", err
  42. }
  43. project.Services = originalServices
  44. updateOneOffServiceConfig(&requestedService, project.Name, opts)
  45. if err := s.waitDependencies(ctx, project, requestedService); err != nil {
  46. return "", err
  47. }
  48. err := s.createContainer(ctx, project, requestedService, requestedService.ContainerName, 1)
  49. if err != nil {
  50. return "", err
  51. }
  52. return requestedService.ContainerName, err
  53. }
  54. func (s *composeService) Run(ctx context.Context, container string, detach bool) error {
  55. if detach {
  56. return s.apiClient.ContainerStart(ctx, container, apitypes.ContainerStartOptions{})
  57. }
  58. cnx, err := s.apiClient.ContainerAttach(ctx, container, apitypes.ContainerAttachOptions{
  59. Stream: true,
  60. Stdin: true,
  61. Stdout: true,
  62. Stderr: true,
  63. Logs: true,
  64. })
  65. if err != nil {
  66. return err
  67. }
  68. defer cnx.Close()
  69. stdout := convert.ContainerStdout{HijackedResponse: cnx}
  70. stdin := convert.ContainerStdin{HijackedResponse: cnx}
  71. readChannel := make(chan error, 10)
  72. writeChannel := make(chan error, 10)
  73. go func() {
  74. _, err := io.Copy(os.Stdout, cnx.Reader)
  75. readChannel <- err
  76. }()
  77. go func() {
  78. _, err := io.Copy(stdin, os.Stdin)
  79. writeChannel <- err
  80. }()
  81. go func() {
  82. <-ctx.Done()
  83. stdout.Close() //nolint:errcheck
  84. stdin.Close() //nolint:errcheck
  85. }()
  86. // start container
  87. err = s.apiClient.ContainerStart(ctx, container, apitypes.ContainerStartOptions{})
  88. if err != nil {
  89. return err
  90. }
  91. for {
  92. select {
  93. case err := <-readChannel:
  94. return err
  95. case err := <-writeChannel:
  96. return err
  97. }
  98. }
  99. }
  100. func updateOneOffServiceConfig(service *types.ServiceConfig, projectName string, opts compose.RunOptions) {
  101. if len(opts.Command) > 0 {
  102. service.Command = opts.Command
  103. }
  104. slug := moby.GenerateRandomID()
  105. service.Scale = 1
  106. service.ContainerName = fmt.Sprintf("%s_%s_run_%s", projectName, service.Name, moby.TruncateID(slug))
  107. service.Labels = types.Labels{
  108. slugLabel: slug,
  109. oneoffLabel: "True",
  110. }
  111. service.Tty = true
  112. service.StdinOpen = true
  113. }