run.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/cli"
  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/containers"
  22. "github.com/docker/compose-cli/progress"
  23. )
  24. type runOptions struct {
  25. Name string
  26. Command []string
  27. WorkingDir string
  28. ConfigPaths []string
  29. Environment []string
  30. Detach bool
  31. Publish []string
  32. Labels []string
  33. Volumes []string
  34. NoDeps bool
  35. Remove bool
  36. }
  37. func runCommand() *cobra.Command {
  38. opts := runOptions{}
  39. runCmd := &cobra.Command{
  40. Use: "run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...] SERVICE [COMMAND] [ARGS...]",
  41. Short: "Run a one-off command on a service.",
  42. Args: cobra.MinimumNArgs(1),
  43. RunE: func(cmd *cobra.Command, args []string) error {
  44. if len(args) > 1 {
  45. opts.Command = args[1:]
  46. }
  47. opts.Name = args[0]
  48. return runRun(cmd.Context(), opts)
  49. },
  50. }
  51. runCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
  52. runCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  53. runCmd.Flags().StringArrayVarP(&opts.Publish, "publish", "p", []string{}, "Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT")
  54. runCmd.Flags().StringVar(&opts.Name, "name", "", "Assign a name to the container")
  55. runCmd.Flags().BoolVar(&opts.NoDeps, "no-deps", false, "Don't start linked services.")
  56. runCmd.Flags().StringArrayVarP(&opts.Labels, "label", "l", []string{}, "Set meta data on a container")
  57. runCmd.Flags().StringArrayVarP(&opts.Volumes, "volume", "v", []string{}, "Volume. Ex: storageaccount/my_share[:/absolute/path/to/target][:ro]")
  58. runCmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID")
  59. runCmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables")
  60. runCmd.Flags().BoolVar(&opts.Remove, "rm", false, "Automatically remove the container when it exits")
  61. runCmd.Flags().SetInterspersed(false)
  62. return runCmd
  63. }
  64. func runRun(ctx context.Context, opts runOptions) error {
  65. projectOpts := composeOptions{
  66. ConfigPaths: opts.ConfigPaths,
  67. WorkingDir: opts.WorkingDir,
  68. Environment: opts.Environment,
  69. }
  70. options, err := projectOpts.toProjectOptions()
  71. if err != nil {
  72. return err
  73. }
  74. project, err := cli.ProjectFromOptions(options)
  75. if err != nil {
  76. return err
  77. }
  78. err = filter(project, []string{opts.Name})
  79. if err != nil {
  80. return err
  81. }
  82. c, err := client.NewWithDefaultLocalBackend(ctx)
  83. if err != nil {
  84. return err
  85. }
  86. containerID, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
  87. return c.ComposeService().CreateOneOffContainer(ctx, project, compose.RunOptions{
  88. Name: opts.Name,
  89. Command: opts.Command,
  90. })
  91. })
  92. if err != nil {
  93. return err
  94. }
  95. // start container and attach to container streams
  96. err = c.ComposeService().Run(ctx, containerID, opts.Detach)
  97. if err != nil {
  98. return err
  99. }
  100. if opts.Detach {
  101. fmt.Printf("%s", containerID)
  102. return nil
  103. }
  104. if opts.Remove {
  105. return c.ContainerService().Delete(ctx, containerID, containers.DeleteRequest{
  106. Force: true,
  107. })
  108. }
  109. return nil
  110. }