create.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
  73. return cmd
  74. }
  75. func (opts createOptions) recreateStrategy() string {
  76. if opts.noRecreate {
  77. return api.RecreateNever
  78. }
  79. if opts.forceRecreate {
  80. return api.RecreateForce
  81. }
  82. return api.RecreateDiverged
  83. }
  84. func (opts createOptions) dependenciesRecreateStrategy() string {
  85. if opts.noRecreate {
  86. return api.RecreateNever
  87. }
  88. if opts.recreateDeps {
  89. return api.RecreateForce
  90. }
  91. return api.RecreateDiverged
  92. }
  93. func (opts createOptions) GetTimeout() *time.Duration {
  94. if opts.timeChanged {
  95. t := time.Duration(opts.timeout) * time.Second
  96. return &t
  97. }
  98. return nil
  99. }
  100. func (opts createOptions) Apply(project *types.Project) {
  101. if opts.pullChanged {
  102. for i, service := range project.Services {
  103. service.PullPolicy = opts.Pull
  104. project.Services[i] = service
  105. }
  106. }
  107. if opts.Build {
  108. for i, service := range project.Services {
  109. if service.Build == nil {
  110. continue
  111. }
  112. service.PullPolicy = types.PullPolicyBuild
  113. project.Services[i] = service
  114. }
  115. }
  116. if opts.noBuild {
  117. for i, service := range project.Services {
  118. service.Build = nil
  119. if service.Image == "" {
  120. service.Image = api.GetImageNameOrDefault(service, project.Name)
  121. }
  122. project.Services[i] = service
  123. }
  124. }
  125. }