run.go 3.4 KB

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