run.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. slug := stringid.GenerateRandomID()
  48. if service.ContainerName == "" {
  49. service.ContainerName = fmt.Sprintf("%s_%s_run_%s", project.Name, service.Name, stringid.TruncateID(slug))
  50. }
  51. service.Scale = 1
  52. service.Restart = ""
  53. if service.Deploy != nil {
  54. service.Deploy.RestartPolicy = nil
  55. }
  56. service.CustomLabels = service.CustomLabels.
  57. Add(api.SlugLabel, slug).
  58. Add(api.OneoffLabel, "True")
  59. if err := s.ensureImagesExists(ctx, project, opts.QuietPull); err != nil { // all dependencies already checked, but might miss service img
  60. return "", err
  61. }
  62. if !opts.NoDeps {
  63. if err := s.waitDependencies(ctx, project, service.DependsOn); err != nil {
  64. return "", err
  65. }
  66. }
  67. observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true)
  68. if err != nil {
  69. return "", err
  70. }
  71. updateServices(&service, observedState)
  72. created, err := s.createContainer(ctx, project, service, service.ContainerName, 1,
  73. opts.Detach && opts.AutoRemove, opts.UseNetworkAliases, opts.Interactive)
  74. if err != nil {
  75. return "", err
  76. }
  77. return created.ID, nil
  78. }
  79. func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts api.RunOptions) {
  80. service.Tty = opts.Tty
  81. service.StdinOpen = opts.Interactive
  82. service.ContainerName = opts.Name
  83. if len(opts.Command) > 0 {
  84. service.Command = opts.Command
  85. }
  86. if len(opts.User) > 0 {
  87. service.User = opts.User
  88. }
  89. if len(opts.WorkingDir) > 0 {
  90. service.WorkingDir = opts.WorkingDir
  91. }
  92. if opts.Entrypoint != nil {
  93. service.Entrypoint = opts.Entrypoint
  94. }
  95. if len(opts.Environment) > 0 {
  96. env := types.NewMappingWithEquals(opts.Environment)
  97. projectEnv := env.Resolve(func(s string) (string, bool) {
  98. v, ok := project.Environment[s]
  99. return v, ok
  100. }).RemoveEmpty()
  101. service.Environment.OverrideBy(projectEnv)
  102. }
  103. for k, v := range opts.Labels {
  104. service.Labels = service.Labels.Add(k, v)
  105. }
  106. }