run.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  61. return "", startDependencies(ctx, c, *project, opts.Service)
  62. })
  63. if err != nil {
  64. return err
  65. }
  66. // start container and attach to container streams
  67. runOpts := compose.RunOptions{
  68. Service: opts.Service,
  69. Command: opts.Command,
  70. Detach: opts.Detach,
  71. AutoRemove: opts.Remove,
  72. Writer: os.Stdout,
  73. Reader: os.Stdin,
  74. }
  75. return c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
  76. }
  77. func startDependencies(ctx context.Context, c *client.Client, project types.Project, requestedServiceName string) error {
  78. dependencies := types.Services{}
  79. var requestedService types.ServiceConfig
  80. for _, service := range project.Services {
  81. if service.Name != requestedServiceName {
  82. dependencies = append(dependencies, service)
  83. } else {
  84. requestedService = service
  85. }
  86. }
  87. project.Services = dependencies
  88. project.DisabledServices = append(project.DisabledServices, requestedService)
  89. if err := c.ComposeService().Create(ctx, &project, compose.CreateOptions{}); err != nil {
  90. return err
  91. }
  92. if err := c.ComposeService().Start(ctx, &project, compose.StartOptions{}); err != nil {
  93. return err
  94. }
  95. return nil
  96. }