run.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "github.com/docker/cli/cli"
  19. cmd "github.com/docker/cli/cli/command/container"
  20. "github.com/docker/compose/v2/pkg/api"
  21. "github.com/docker/docker/pkg/stringid"
  22. )
  23. func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) {
  24. containerID, err := s.prepareRun(ctx, project, opts)
  25. if err != nil {
  26. return 0, err
  27. }
  28. start := cmd.NewStartOptions()
  29. start.OpenStdin = !opts.Detach && opts.Interactive
  30. start.Attach = !opts.Detach
  31. start.Containers = []string{containerID}
  32. err = cmd.RunStart(s.dockerCli, &start)
  33. if sterr, ok := err.(cli.StatusError); ok {
  34. return sterr.StatusCode, nil
  35. }
  36. return 0, err
  37. }
  38. func (s *composeService) prepareRun(ctx context.Context, project *types.Project, opts api.RunOptions) (string, error) {
  39. if err := prepareVolumes(project); err != nil { // all dependencies already checked, but might miss service img
  40. return "", err
  41. }
  42. service, err := project.GetService(opts.Service)
  43. if err != nil {
  44. return "", err
  45. }
  46. applyRunOptions(project, &service, opts)
  47. if err := s.dockerCli.In().CheckTty(opts.Interactive, service.Tty); err != nil {
  48. return "", err
  49. }
  50. slug := stringid.GenerateRandomID()
  51. if service.ContainerName == "" {
  52. service.ContainerName = fmt.Sprintf("%s_%s_run_%s", project.Name, service.Name, stringid.TruncateID(slug))
  53. }
  54. service.Scale = 1
  55. service.Restart = ""
  56. if service.Deploy != nil {
  57. service.Deploy.RestartPolicy = nil
  58. }
  59. service.CustomLabels = service.CustomLabels.
  60. Add(api.SlugLabel, slug).
  61. Add(api.OneoffLabel, "True")
  62. if err := s.ensureImagesExists(ctx, project, opts.QuietPull); err != nil { // all dependencies already checked, but might miss service img
  63. return "", err
  64. }
  65. if !opts.NoDeps {
  66. if err := s.waitDependencies(ctx, project, service.DependsOn); err != nil {
  67. return "", err
  68. }
  69. }
  70. observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true)
  71. if err != nil {
  72. return "", err
  73. }
  74. updateServices(&service, observedState)
  75. created, err := s.createContainer(ctx, project, service, service.ContainerName, 1,
  76. opts.AutoRemove, opts.UseNetworkAliases, opts.Interactive)
  77. if err != nil {
  78. return "", err
  79. }
  80. return created.ID, nil
  81. }
  82. func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts api.RunOptions) {
  83. service.Tty = opts.Tty
  84. service.StdinOpen = opts.Interactive
  85. service.ContainerName = opts.Name
  86. if len(opts.Command) > 0 {
  87. service.Command = opts.Command
  88. }
  89. if len(opts.User) > 0 {
  90. service.User = opts.User
  91. }
  92. if len(opts.WorkingDir) > 0 {
  93. service.WorkingDir = opts.WorkingDir
  94. }
  95. if opts.Entrypoint != nil {
  96. service.Entrypoint = opts.Entrypoint
  97. }
  98. if len(opts.Environment) > 0 {
  99. env := types.NewMappingWithEquals(opts.Environment)
  100. projectEnv := env.Resolve(func(s string) (string, bool) {
  101. if _, ok := service.Environment[s]; ok {
  102. return "", false
  103. }
  104. v, ok := project.Environment[s]
  105. return v, ok
  106. }).RemoveEmpty()
  107. service.Environment.OverrideBy(projectEnv)
  108. }
  109. for k, v := range opts.Labels {
  110. service.Labels = service.Labels.Add(k, v)
  111. }
  112. }