up.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "errors"
  17. "fmt"
  18. "os"
  19. "github.com/docker/compose-cli/api/client"
  20. "github.com/docker/compose-cli/api/compose"
  21. "github.com/docker/compose-cli/api/context/store"
  22. "github.com/docker/compose-cli/api/progress"
  23. "github.com/docker/compose-cli/cli/formatter"
  24. "github.com/compose-spec/compose-go/types"
  25. "github.com/spf13/cobra"
  26. )
  27. type upOptions struct {
  28. composeOptions
  29. removeOrphans bool
  30. }
  31. func upCommand(contextType string) *cobra.Command {
  32. opts := upOptions{}
  33. upCmd := &cobra.Command{
  34. Use: "up [SERVICE...]",
  35. Short: "Create and start containers",
  36. RunE: func(cmd *cobra.Command, args []string) error {
  37. switch contextType {
  38. case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
  39. return runCreateStart(cmd.Context(), opts, args)
  40. default:
  41. return runUp(cmd.Context(), opts, args)
  42. }
  43. },
  44. }
  45. upCmd.Flags().StringVarP(&opts.ProjectName, "project-name", "p", "", "Project name")
  46. upCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
  47. upCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  48. upCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
  49. upCmd.Flags().StringVar(&opts.EnvFile, "env-file", "", "Specify an alternate environment file.")
  50. upCmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Detached mode: Run containers in the background")
  51. upCmd.Flags().BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
  52. upCmd.Flags().BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
  53. if contextType == store.AciContextType {
  54. upCmd.Flags().StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name")
  55. }
  56. return upCmd
  57. }
  58. func runUp(ctx context.Context, opts upOptions, services []string) error {
  59. c, project, err := setup(ctx, opts.composeOptions, services)
  60. if err != nil {
  61. return err
  62. }
  63. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  64. return "", c.ComposeService().Up(ctx, project, compose.UpOptions{
  65. Detach: opts.Detach,
  66. })
  67. })
  68. return err
  69. }
  70. func runCreateStart(ctx context.Context, opts upOptions, services []string) error {
  71. c, project, err := setup(ctx, opts.composeOptions, services)
  72. if err != nil {
  73. return err
  74. }
  75. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  76. return "", c.ComposeService().Create(ctx, project, compose.CreateOptions{
  77. RemoveOrphans: opts.removeOrphans,
  78. })
  79. })
  80. if err != nil {
  81. return err
  82. }
  83. var consumer compose.LogConsumer
  84. if !opts.Detach {
  85. consumer = formatter.NewLogConsumer(ctx, os.Stdout)
  86. }
  87. err = c.ComposeService().Start(ctx, project, consumer)
  88. if errors.Is(ctx.Err(), context.Canceled) {
  89. fmt.Println("Gracefully stopping...")
  90. ctx = context.Background()
  91. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  92. return "", c.ComposeService().Down(ctx, project.Name, compose.DownOptions{})
  93. })
  94. }
  95. return err
  96. }
  97. func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
  98. c, err := client.NewWithDefaultLocalBackend(ctx)
  99. if err != nil {
  100. return nil, nil, err
  101. }
  102. project, err := opts.toProject()
  103. if err != nil {
  104. return nil, nil, err
  105. }
  106. if opts.DomainName != "" {
  107. // arbitrarily set the domain name on the first service ; ACI backend will expose the entire project
  108. project.Services[0].DomainName = opts.DomainName
  109. }
  110. if opts.Build {
  111. for _, service := range project.Services {
  112. service.PullPolicy = types.PullPolicyBuild
  113. }
  114. }
  115. err = filter(project, services)
  116. if err != nil {
  117. return nil, nil, err
  118. }
  119. return c, project, nil
  120. }