run.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/pkg/api"
  18. "github.com/compose-spec/compose-go/types"
  19. moby "github.com/docker/docker/api/types"
  20. "github.com/docker/docker/api/types/container"
  21. "github.com/docker/docker/api/types/filters"
  22. "github.com/docker/docker/pkg/stringid"
  23. )
  24. func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) {
  25. observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true)
  26. if err != nil {
  27. return 0, err
  28. }
  29. containerState := NewContainersState(observedState)
  30. ctx = context.WithValue(ctx, ContainersKey{}, containerState)
  31. service, err := project.GetService(opts.Service)
  32. if err != nil {
  33. return 0, err
  34. }
  35. applyRunOptions(project, &service, opts)
  36. slug := stringid.GenerateRandomID()
  37. if service.ContainerName == "" {
  38. service.ContainerName = fmt.Sprintf("%s_%s_run_%s", project.Name, service.Name, stringid.TruncateID(slug))
  39. }
  40. service.Scale = 1
  41. service.StdinOpen = true
  42. service.Labels = service.Labels.Add(api.SlugLabel, slug)
  43. service.Labels = service.Labels.Add(api.OneoffLabel, "True")
  44. if err := s.ensureImagesExists(ctx, project, observedState, false); err != nil { // all dependencies already checked, but might miss service img
  45. return 0, err
  46. }
  47. if err := s.waitDependencies(ctx, project, service); err != nil {
  48. return 0, err
  49. }
  50. if err := s.createContainer(ctx, project, service, service.ContainerName, 1, opts.AutoRemove, opts.UseNetworkAliases); err != nil {
  51. return 0, err
  52. }
  53. containerID := service.ContainerName
  54. if opts.Detach {
  55. err := s.apiClient.ContainerStart(ctx, containerID, moby.ContainerStartOptions{})
  56. if err != nil {
  57. return 0, err
  58. }
  59. fmt.Fprintln(opts.Writer, containerID)
  60. return 0, nil
  61. }
  62. containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  63. Filters: filters.NewArgs(slugFilter(slug)),
  64. All: true,
  65. })
  66. if err != nil {
  67. return 0, err
  68. }
  69. oneoffContainer := containers[0]
  70. restore, err := s.attachContainerStreams(ctx, oneoffContainer.ID, service.Tty, opts.Reader, opts.Writer)
  71. if err != nil {
  72. return 0, err
  73. }
  74. defer restore()
  75. statusC, errC := s.apiClient.ContainerWait(context.Background(), oneoffContainer.ID, container.WaitConditionNextExit)
  76. err = s.apiClient.ContainerStart(ctx, containerID, moby.ContainerStartOptions{})
  77. if err != nil {
  78. return 0, err
  79. }
  80. s.monitorTTySize(ctx, containerID, s.apiClient.ContainerResize)
  81. select {
  82. case status := <-statusC:
  83. return int(status.StatusCode), nil
  84. case err := <-errC:
  85. return 0, err
  86. }
  87. }
  88. func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts api.RunOptions) {
  89. service.Tty = opts.Tty
  90. service.ContainerName = opts.Name
  91. if len(opts.Command) > 0 {
  92. service.Command = opts.Command
  93. }
  94. if len(opts.User) > 0 {
  95. service.User = opts.User
  96. }
  97. if len(opts.WorkingDir) > 0 {
  98. service.WorkingDir = opts.WorkingDir
  99. }
  100. if len(opts.Entrypoint) > 0 {
  101. service.Entrypoint = opts.Entrypoint
  102. }
  103. if len(opts.Environment) > 0 {
  104. env := types.NewMappingWithEquals(opts.Environment)
  105. projectEnv := env.Resolve(func(s string) (string, bool) {
  106. v, ok := project.Environment[s]
  107. return v, ok
  108. }).RemoveEmpty()
  109. service.Environment.OverrideBy(projectEnv)
  110. }
  111. for k, v := range opts.Labels {
  112. service.Labels.Add(k, v)
  113. }
  114. }