run.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/api/containers"
  22. "github.com/docker/api/cli/options/run"
  23. "github.com/docker/api/client"
  24. "github.com/docker/api/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. return cmd
  45. }
  46. func runRun(ctx context.Context, image string, opts run.Opts) error {
  47. c, err := client.New(ctx)
  48. if err != nil {
  49. return err
  50. }
  51. containerConfig, err := opts.ToContainerConfig(image)
  52. if err != nil {
  53. return err
  54. }
  55. err = progress.Run(ctx, func(ctx context.Context) error {
  56. return c.ContainerService().Run(ctx, containerConfig)
  57. })
  58. if err != nil {
  59. return err
  60. }
  61. if !opts.Detach {
  62. var con io.Writer = os.Stdout
  63. if c, err := console.ConsoleFromFile(os.Stdout); err == nil {
  64. con = c
  65. }
  66. req := containers.LogsRequest{
  67. Follow: true,
  68. Writer: con,
  69. }
  70. return c.ContainerService().Logs(ctx, opts.Name, req)
  71. }
  72. fmt.Println(opts.Name)
  73. return nil
  74. }