run.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/docker/compose-cli/api/compose"
  18. "github.com/compose-spec/compose-go/types"
  19. apitypes "github.com/docker/docker/api/types"
  20. "github.com/docker/docker/api/types/container"
  21. "github.com/docker/docker/api/types/filters"
  22. moby "github.com/docker/docker/pkg/stringid"
  23. )
  24. func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, 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 0, err
  45. }
  46. if err := s.waitDependencies(ctx, project, requestedService); err != nil {
  47. return 0, err
  48. }
  49. if err := s.createContainer(ctx, project, requestedService, requestedService.ContainerName, 1, opts.AutoRemove); err != nil {
  50. return 0, 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 0, err
  57. }
  58. fmt.Fprintln(opts.Writer, containerID)
  59. return 0, 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 0, err
  69. }
  70. oneoffContainer := containers[0]
  71. err = s.attachContainerStreams(ctx, oneoffContainer, true, opts.Reader, opts.Writer)
  72. if err != nil {
  73. return 0, err
  74. }
  75. err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
  76. if err != nil {
  77. return 0, err
  78. }
  79. statusC, errC := s.apiClient.ContainerWait(context.Background(), oneoffContainer.ID, container.WaitConditionNotRunning)
  80. select {
  81. case status := <-statusC:
  82. return int(status.StatusCode), nil
  83. case err := <-errC:
  84. return 0, err
  85. }
  86. }