run.go 3.6 KB

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