run.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "os"
  18. "github.com/compose-spec/compose-go/types"
  19. "github.com/docker/compose-cli/api/compose"
  20. "github.com/docker/compose-cli/utils"
  21. apitypes "github.com/docker/docker/api/types"
  22. "github.com/docker/docker/api/types/filters"
  23. "golang.org/x/sync/errgroup"
  24. moby "github.com/docker/docker/pkg/stringid"
  25. )
  26. func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
  27. originalServices := project.Services
  28. var requestedService types.ServiceConfig
  29. for _, service := range originalServices {
  30. if service.Name == opts.Name {
  31. requestedService = service
  32. }
  33. }
  34. project.Services = originalServices
  35. if len(opts.Command) > 0 {
  36. requestedService.Command = opts.Command
  37. }
  38. requestedService.Scale = 1
  39. requestedService.Tty = true
  40. requestedService.StdinOpen = true
  41. slug := moby.GenerateRandomID()
  42. requestedService.ContainerName = fmt.Sprintf("%s_%s_run_%s", project.Name, requestedService.Name, moby.TruncateID(slug))
  43. requestedService.Labels = requestedService.Labels.Add(slugLabel, slug)
  44. requestedService.Labels = requestedService.Labels.Add(oneoffLabel, "True")
  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. containerID := requestedService.ContainerName
  53. if opts.Detach {
  54. return containerID, s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
  55. }
  56. containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{
  57. Filters: filters.NewArgs(
  58. projectFilter(project.Name),
  59. ),
  60. All: true,
  61. })
  62. if err != nil {
  63. return "", err
  64. }
  65. var oneoffContainer apitypes.Container
  66. for _, container := range containers {
  67. if utils.StringContains(container.Names, "/"+containerID) {
  68. oneoffContainer = container
  69. }
  70. }
  71. eg := errgroup.Group{}
  72. eg.Go(func() error {
  73. return s.attachContainerStreams(ctx, oneoffContainer, true, os.Stdin, os.Stdout)
  74. })
  75. if err != nil {
  76. return "", err
  77. }
  78. err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
  79. if err != nil {
  80. return "", err
  81. }
  82. err = eg.Wait()
  83. return containerID, err
  84. }