run.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "os"
  17. "github.com/compose-spec/compose-go/types"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/compose-cli/api/client"
  20. "github.com/docker/compose-cli/api/compose"
  21. "github.com/docker/compose-cli/api/progress"
  22. "github.com/docker/compose-cli/cli/cmd"
  23. )
  24. type runOptions struct {
  25. *composeOptions
  26. Service string
  27. Command []string
  28. Environment []string
  29. Detach bool
  30. Remove bool
  31. }
  32. func runCommand(p *projectOptions) *cobra.Command {
  33. opts := runOptions{
  34. composeOptions: &composeOptions{
  35. projectOptions: p,
  36. },
  37. }
  38. runCmd := &cobra.Command{
  39. Use: "run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...] SERVICE [COMMAND] [ARGS...]",
  40. Short: "Run a one-off command on a service.",
  41. Args: cobra.MinimumNArgs(1),
  42. RunE: func(cmd *cobra.Command, args []string) error {
  43. if len(args) > 1 {
  44. opts.Command = args[1:]
  45. }
  46. opts.Service = args[0]
  47. return runRun(cmd.Context(), opts)
  48. },
  49. }
  50. runCmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID")
  51. runCmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables")
  52. runCmd.Flags().BoolVar(&opts.Remove, "rm", false, "Automatically remove the container when it exits")
  53. runCmd.Flags().SetInterspersed(false)
  54. return runCmd
  55. }
  56. func runRun(ctx context.Context, opts runOptions) error {
  57. c, project, err := setup(ctx, *opts.composeOptions, []string{opts.Service})
  58. if err != nil {
  59. return err
  60. }
  61. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  62. return "", startDependencies(ctx, c, *project, opts.Service)
  63. })
  64. if err != nil {
  65. return err
  66. }
  67. // start container and attach to container streams
  68. runOpts := compose.RunOptions{
  69. Service: opts.Service,
  70. Command: opts.Command,
  71. Detach: opts.Detach,
  72. AutoRemove: opts.Remove,
  73. Writer: os.Stdout,
  74. Reader: os.Stdin,
  75. }
  76. exitCode, err := c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
  77. if exitCode != 0 {
  78. return cmd.ExitCodeError{ExitCode: exitCode}
  79. }
  80. return err
  81. }
  82. func startDependencies(ctx context.Context, c *client.Client, project types.Project, requestedServiceName string) error {
  83. dependencies := types.Services{}
  84. var requestedService types.ServiceConfig
  85. for _, service := range project.Services {
  86. if service.Name != requestedServiceName {
  87. dependencies = append(dependencies, service)
  88. } else {
  89. requestedService = service
  90. }
  91. }
  92. project.Services = dependencies
  93. project.DisabledServices = append(project.DisabledServices, requestedService)
  94. if err := c.ComposeService().Create(ctx, &project, compose.CreateOptions{}); err != nil {
  95. return err
  96. }
  97. if err := c.ComposeService().Start(ctx, &project, compose.StartOptions{}); err != nil {
  98. return err
  99. }
  100. return nil
  101. }