create.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "time"
  18. "github.com/compose-spec/compose-go/types"
  19. "github.com/spf13/cobra"
  20. "github.com/docker/compose/v2/pkg/api"
  21. )
  22. type createOptions struct {
  23. Build bool
  24. noBuild bool
  25. Pull string
  26. pullChanged bool
  27. removeOrphans bool
  28. ignoreOrphans bool
  29. forceRecreate bool
  30. noRecreate bool
  31. recreateDeps bool
  32. noInherit bool
  33. timeChanged bool
  34. timeout int
  35. quietPull bool
  36. }
  37. func createCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
  38. opts := createOptions{}
  39. cmd := &cobra.Command{
  40. Use: "create [OPTIONS] [SERVICE...]",
  41. Short: "Creates containers for a service.",
  42. PreRunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
  43. opts.pullChanged = cmd.Flags().Changed("pull")
  44. if opts.Build && opts.noBuild {
  45. return fmt.Errorf("--build and --no-build are incompatible")
  46. }
  47. if opts.forceRecreate && opts.noRecreate {
  48. return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
  49. }
  50. return nil
  51. }),
  52. RunE: p.WithProject(func(ctx context.Context, project *types.Project) error {
  53. opts.Apply(project)
  54. return backend.Create(ctx, project, api.CreateOptions{
  55. RemoveOrphans: opts.removeOrphans,
  56. IgnoreOrphans: opts.ignoreOrphans,
  57. Recreate: opts.recreateStrategy(),
  58. RecreateDependencies: opts.dependenciesRecreateStrategy(),
  59. Inherit: !opts.noInherit,
  60. Timeout: opts.GetTimeout(),
  61. QuietPull: false,
  62. })
  63. }),
  64. ValidArgsFunction: completeServiceNames(p),
  65. }
  66. flags := cmd.Flags()
  67. flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
  68. flags.BoolVar(&opts.noBuild, "no-build", false, "Don't build an image, even if it's missing.")
  69. flags.StringVar(&opts.Pull, "pull", "missing", `Pull image before running ("always"|"missing"|"never")`)
  70. flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
  71. flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
  72. return cmd
  73. }
  74. func (opts createOptions) recreateStrategy() string {
  75. if opts.noRecreate {
  76. return api.RecreateNever
  77. }
  78. if opts.forceRecreate {
  79. return api.RecreateForce
  80. }
  81. return api.RecreateDiverged
  82. }
  83. func (opts createOptions) dependenciesRecreateStrategy() string {
  84. if opts.noRecreate {
  85. return api.RecreateNever
  86. }
  87. if opts.recreateDeps {
  88. return api.RecreateForce
  89. }
  90. return api.RecreateDiverged
  91. }
  92. func (opts createOptions) GetTimeout() *time.Duration {
  93. if opts.timeChanged {
  94. t := time.Duration(opts.timeout) * time.Second
  95. return &t
  96. }
  97. return nil
  98. }
  99. func (opts createOptions) Apply(project *types.Project) {
  100. if opts.pullChanged {
  101. for i, service := range project.Services {
  102. service.PullPolicy = opts.Pull
  103. project.Services[i] = service
  104. }
  105. }
  106. if opts.Build {
  107. for i, service := range project.Services {
  108. if service.Build == nil {
  109. continue
  110. }
  111. service.PullPolicy = types.PullPolicyBuild
  112. project.Services[i] = service
  113. }
  114. }
  115. if opts.noBuild {
  116. for i, service := range project.Services {
  117. service.Build = nil
  118. if service.Image == "" {
  119. service.Image = api.GetImageNameOrDefault(service, project.Name)
  120. }
  121. project.Services[i] = service
  122. }
  123. }
  124. }