run.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. cmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables")
  45. return cmd
  46. }
  47. func runRun(ctx context.Context, image string, opts run.Opts) error {
  48. c, err := client.New(ctx)
  49. if err != nil {
  50. return err
  51. }
  52. containerConfig, err := opts.ToContainerConfig(image)
  53. if err != nil {
  54. return err
  55. }
  56. err = progress.Run(ctx, func(ctx context.Context) error {
  57. return c.ContainerService().Run(ctx, containerConfig)
  58. })
  59. if err != nil {
  60. return err
  61. }
  62. if !opts.Detach {
  63. var con io.Writer = os.Stdout
  64. if c, err := console.ConsoleFromFile(os.Stdout); err == nil {
  65. con = c
  66. }
  67. req := containers.LogsRequest{
  68. Follow: true,
  69. Writer: con,
  70. }
  71. return c.ContainerService().Logs(ctx, opts.Name, req)
  72. }
  73. fmt.Println(opts.Name)
  74. return nil
  75. }