run.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "github.com/compose-spec/compose-go/types"
  18. apitypes "github.com/docker/docker/api/types"
  19. "github.com/docker/docker/api/types/filters"
  20. "golang.org/x/sync/errgroup"
  21. "github.com/docker/compose-cli/api/compose"
  22. moby "github.com/docker/docker/pkg/stringid"
  23. )
  24. func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
  25. originalServices := project.Services
  26. var requestedService types.ServiceConfig
  27. for _, service := range originalServices {
  28. if service.Name == opts.Service {
  29. requestedService = service
  30. }
  31. }
  32. project.Services = originalServices
  33. if len(opts.Command) > 0 {
  34. requestedService.Command = opts.Command
  35. }
  36. requestedService.Scale = 1
  37. requestedService.Tty = true
  38. requestedService.StdinOpen = true
  39. slug := moby.GenerateRandomID()
  40. requestedService.ContainerName = fmt.Sprintf("%s_%s_run_%s", project.Name, requestedService.Name, moby.TruncateID(slug))
  41. requestedService.Labels = requestedService.Labels.Add(slugLabel, slug)
  42. requestedService.Labels = requestedService.Labels.Add(oneoffLabel, "True")
  43. if err := s.ensureImagesExists(ctx, project); err != nil { // all dependencies already checked, but might miss requestedService img
  44. return err
  45. }
  46. if err := s.waitDependencies(ctx, project, requestedService); err != nil {
  47. return err
  48. }
  49. if err := s.createContainer(ctx, project, requestedService, requestedService.ContainerName, 1, opts.AutoRemove); err != nil {
  50. return err
  51. }
  52. containerID := requestedService.ContainerName
  53. if opts.Detach {
  54. err := s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
  55. if err != nil {
  56. return err
  57. }
  58. fmt.Fprintln(opts.Writer, containerID)
  59. return nil
  60. }
  61. containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{
  62. Filters: filters.NewArgs(
  63. filters.Arg("label", fmt.Sprintf("%s=%s", slugLabel, slug)),
  64. ),
  65. All: true,
  66. })
  67. if err != nil {
  68. return err
  69. }
  70. oneoffContainer := containers[0]
  71. eg := errgroup.Group{}
  72. eg.Go(func() error {
  73. return s.attachContainerStreams(ctx, oneoffContainer, true, opts.Reader, opts.Writer)
  74. })
  75. if err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{}); err != nil {
  76. return err
  77. }
  78. return eg.Wait()
  79. }