run.go 3.7 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 run
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "os"
  19. "github.com/containerd/console"
  20. "github.com/spf13/cobra"
  21. "github.com/docker/compose-cli/api/client"
  22. "github.com/docker/compose-cli/api/containers"
  23. "github.com/docker/compose-cli/cli/options/run"
  24. "github.com/docker/compose-cli/context/store"
  25. "github.com/docker/compose-cli/progress"
  26. )
  27. // Command runs a container
  28. func Command(contextType string) *cobra.Command {
  29. var opts run.Opts
  30. cmd := &cobra.Command{
  31. Use: "run",
  32. Short: "Run a container",
  33. Args: cobra.MinimumNArgs(1),
  34. RunE: func(cmd *cobra.Command, args []string) error {
  35. if len(args) > 1 {
  36. opts.Command = args[1:]
  37. }
  38. return runRun(cmd.Context(), args[0], contextType, opts)
  39. },
  40. }
  41. cmd.Flags().SetInterspersed(false)
  42. cmd.Flags().StringArrayVarP(&opts.Publish, "publish", "p", []string{}, "Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT")
  43. cmd.Flags().StringVar(&opts.Name, "name", "", "Assign a name to the container")
  44. cmd.Flags().StringArrayVarP(&opts.Labels, "label", "l", []string{}, "Set meta data on a container")
  45. cmd.Flags().StringArrayVarP(&opts.Volumes, "volume", "v", []string{}, "Volume. Ex: storageaccount/my_share[:/absolute/path/to/target][:ro]")
  46. cmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID")
  47. cmd.Flags().Float64Var(&opts.Cpus, "cpus", 1., "Number of CPUs")
  48. cmd.Flags().VarP(&opts.Memory, "memory", "m", "Memory limit")
  49. cmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables")
  50. cmd.Flags().StringArrayVar(&opts.EnvironmentFiles, "env-file", []string{}, "Path to environment files to be translated as environment variables")
  51. cmd.Flags().StringVarP(&opts.RestartPolicyCondition, "restart", "", containers.RestartPolicyRunNo, "Restart policy to apply when a container exits (no|always|on-failure)")
  52. cmd.Flags().BoolVar(&opts.Rm, "rm", false, "Automatically remove the container when it exits")
  53. if contextType == store.AciContextType {
  54. cmd.Flags().StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name")
  55. }
  56. switch contextType {
  57. case store.LocalContextType:
  58. default:
  59. _ = cmd.Flags().MarkHidden("rm")
  60. }
  61. return cmd
  62. }
  63. func runRun(ctx context.Context, image string, contextType string, opts run.Opts) error {
  64. switch contextType {
  65. case store.LocalContextType:
  66. default:
  67. if opts.Rm {
  68. return fmt.Errorf(`flag "--rm" is not yet implemented for %q context type`, contextType)
  69. }
  70. }
  71. c, err := client.New(ctx)
  72. if err != nil {
  73. return err
  74. }
  75. containerConfig, err := opts.ToContainerConfig(image)
  76. if err != nil {
  77. return err
  78. }
  79. result, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
  80. return containerConfig.ID, c.ContainerService().Run(ctx, containerConfig)
  81. })
  82. if err != nil {
  83. return err
  84. }
  85. if !opts.Detach {
  86. var con io.Writer = os.Stdout
  87. req := containers.LogsRequest{
  88. Follow: true,
  89. }
  90. if c, err := console.ConsoleFromFile(os.Stdout); err == nil {
  91. size, err := c.Size()
  92. if err != nil {
  93. return err
  94. }
  95. req.Width = int(size.Width)
  96. con = c
  97. }
  98. req.Writer = con
  99. return c.ContainerService().Logs(ctx, opts.Name, req)
  100. }
  101. fmt.Println(result)
  102. return nil
  103. }