run.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. *composeOptions
  25. Service string
  26. Command []string
  27. Environment []string
  28. Detach bool
  29. Remove bool
  30. }
  31. func runCommand(p *projectOptions) *cobra.Command {
  32. opts := runOptions{
  33. composeOptions: &composeOptions{
  34. projectOptions: p,
  35. },
  36. }
  37. runCmd := &cobra.Command{
  38. Use: "run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...] SERVICE [COMMAND] [ARGS...]",
  39. Short: "Run a one-off command on a service.",
  40. Args: cobra.MinimumNArgs(1),
  41. RunE: func(cmd *cobra.Command, args []string) error {
  42. if len(args) > 1 {
  43. opts.Command = args[1:]
  44. }
  45. opts.Service = args[0]
  46. return runRun(cmd.Context(), opts)
  47. },
  48. }
  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. c, project, err := setup(ctx, *opts.composeOptions, []string{opts.Service})
  57. if err != nil {
  58. return err
  59. }
  60. originalServices := project.Services
  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. project.Services = originalServices
  68. // start container and attach to container streams
  69. runOpts := compose.RunOptions{
  70. Service: opts.Service,
  71. Command: opts.Command,
  72. Detach: opts.Detach,
  73. AutoRemove: opts.Remove,
  74. Writer: os.Stdout,
  75. Reader: os.Stdin,
  76. }
  77. return c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
  78. }
  79. func startDependencies(ctx context.Context, c *client.Client, project *types.Project, requestedService string) error {
  80. originalServices := project.Services
  81. dependencies := types.Services{}
  82. for _, service := range originalServices {
  83. if service.Name != requestedService {
  84. dependencies = append(dependencies, service)
  85. }
  86. }
  87. project.Services = dependencies
  88. if err := c.ComposeService().Create(ctx, project, compose.CreateOptions{}); err != nil {
  89. return err
  90. }
  91. if err := c.ComposeService().Start(ctx, project, compose.StartOptions{}); err != nil {
  92. return err
  93. }
  94. return nil
  95. }