run.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
  26. originalServices := project.Services
  27. var requestedService types.ServiceConfig
  28. for _, service := range originalServices {
  29. if service.Name == opts.Name {
  30. requestedService = service
  31. }
  32. }
  33. project.Services = originalServices
  34. if len(opts.Command) > 0 {
  35. requestedService.Command = opts.Command
  36. }
  37. requestedService.Scale = 1
  38. requestedService.Tty = true
  39. requestedService.StdinOpen = true
  40. slug := moby.GenerateRandomID()
  41. requestedService.ContainerName = fmt.Sprintf("%s_%s_run_%s", project.Name, requestedService.Name, moby.TruncateID(slug))
  42. requestedService.Labels = requestedService.Labels.Add(slugLabel, slug)
  43. requestedService.Labels = requestedService.Labels.Add(oneoffLabel, "True")
  44. if err := s.waitDependencies(ctx, project, requestedService); err != nil {
  45. return "", err
  46. }
  47. err := s.createContainer(ctx, project, requestedService, requestedService.ContainerName, 1)
  48. if err != nil {
  49. return "", err
  50. }
  51. containerID := requestedService.ContainerName
  52. if opts.Detach {
  53. return containerID, s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
  54. }
  55. cnx, err := s.apiClient.ContainerAttach(ctx, containerID, apitypes.ContainerAttachOptions{
  56. Stream: true,
  57. Stdin: true,
  58. Stdout: true,
  59. Stderr: true,
  60. Logs: true,
  61. })
  62. if err != nil {
  63. return containerID, 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, containerID, apitypes.ContainerStartOptions{})
  85. if err != nil {
  86. return containerID, err
  87. }
  88. for {
  89. select {
  90. case err := <-readChannel:
  91. return containerID, err
  92. case err := <-writeChannel:
  93. return containerID, err
  94. }
  95. }
  96. }