run.go 2.8 KB

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