run.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.ExactArgs(1),
  34. RunE: func(cmd *cobra.Command, args []string) error {
  35. return runRun(cmd.Context(), args[0], opts)
  36. },
  37. }
  38. cmd.Flags().StringArrayVarP(&opts.Publish, "publish", "p", []string{}, "Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT")
  39. cmd.Flags().StringVar(&opts.Name, "name", "", "Assign a name to the container")
  40. cmd.Flags().StringArrayVarP(&opts.Labels, "label", "l", []string{}, "Set meta data on a container")
  41. cmd.Flags().StringArrayVarP(&opts.Volumes, "volume", "v", []string{}, "Volume. Ex: storageaccount/my_share[:/absolute/path/to/target][:ro]")
  42. cmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID")
  43. cmd.Flags().Float64Var(&opts.Cpus, "cpus", 1., "Number of CPUs")
  44. cmd.Flags().VarP(&opts.Memory, "memory", "m", "Memory limit")
  45. cmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables")
  46. cmd.Flags().StringVarP(&opts.RestartPolicyCondition, "restart", "", containers.RestartPolicyNone, "Restart policy to apply when a container exits")
  47. if contextType == store.AciContextType {
  48. cmd.Flags().StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name")
  49. }
  50. return cmd
  51. }
  52. func runRun(ctx context.Context, image string, opts run.Opts) error {
  53. c, err := client.New(ctx)
  54. if err != nil {
  55. return err
  56. }
  57. containerConfig, err := opts.ToContainerConfig(image)
  58. if err != nil {
  59. return err
  60. }
  61. result, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
  62. return containerConfig.ID, c.ContainerService().Run(ctx, containerConfig)
  63. })
  64. if err != nil {
  65. return err
  66. }
  67. if !opts.Detach {
  68. var con io.Writer = os.Stdout
  69. req := containers.LogsRequest{
  70. Follow: true,
  71. }
  72. if c, err := console.ConsoleFromFile(os.Stdout); err == nil {
  73. size, err := c.Size()
  74. if err != nil {
  75. return err
  76. }
  77. req.Width = int(size.Width)
  78. con = c
  79. }
  80. req.Writer = con
  81. return c.ContainerService().Logs(ctx, opts.Name, req)
  82. }
  83. fmt.Println(result)
  84. return nil
  85. }