run.go 3.2 KB

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