create.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package volume
  2. import (
  3. "fmt"
  4. "golang.org/x/net/context"
  5. volumetypes "github.com/docker/docker/api/types/volume"
  6. "github.com/docker/docker/cli"
  7. "github.com/docker/docker/cli/command"
  8. "github.com/docker/docker/opts"
  9. runconfigopts "github.com/docker/docker/runconfig/opts"
  10. "github.com/spf13/cobra"
  11. )
  12. type createOptions struct {
  13. name string
  14. driver string
  15. driverOpts opts.MapOpts
  16. labels opts.ListOpts
  17. }
  18. func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
  19. opts := createOptions{
  20. driverOpts: *opts.NewMapOpts(nil, nil),
  21. labels: opts.NewListOpts(runconfigopts.ValidateEnv),
  22. }
  23. cmd := &cobra.Command{
  24. Use: "create [OPTIONS] [VOLUME]",
  25. Short: "Create a volume",
  26. Long: createDescription,
  27. Args: cli.RequiresMaxArgs(1),
  28. RunE: func(cmd *cobra.Command, args []string) error {
  29. if len(args) == 1 {
  30. if opts.name != "" {
  31. fmt.Fprint(dockerCli.Err(), "Conflicting options: either specify --name or provide positional arg, not both\n")
  32. return cli.StatusError{StatusCode: 1}
  33. }
  34. opts.name = args[0]
  35. }
  36. return runCreate(dockerCli, opts)
  37. },
  38. }
  39. flags := cmd.Flags()
  40. flags.StringVarP(&opts.driver, "driver", "d", "local", "Specify volume driver name")
  41. flags.StringVar(&opts.name, "name", "", "Specify volume name")
  42. flags.Lookup("name").Hidden = true
  43. flags.VarP(&opts.driverOpts, "opt", "o", "Set driver specific options")
  44. flags.Var(&opts.labels, "label", "Set metadata for a volume")
  45. return cmd
  46. }
  47. func runCreate(dockerCli *command.DockerCli, opts createOptions) error {
  48. client := dockerCli.Client()
  49. volReq := volumetypes.VolumesCreateBody{
  50. Driver: opts.driver,
  51. DriverOpts: opts.driverOpts.GetAll(),
  52. Name: opts.name,
  53. Labels: runconfigopts.ConvertKVStringsToMap(opts.labels.GetAll()),
  54. }
  55. vol, err := client.VolumeCreate(context.Background(), volReq)
  56. if err != nil {
  57. return err
  58. }
  59. fmt.Fprintf(dockerCli.Out(), "%s\n", vol.Name)
  60. return nil
  61. }
  62. var createDescription = `
  63. Creates a new volume that containers can consume and store data in. If a name
  64. is not specified, Docker generates a random name. You create a volume and then
  65. configure the container to use it, for example:
  66. $ docker volume create hello
  67. hello
  68. $ docker run -d -v hello:/world busybox ls /world
  69. The mount is created inside the container's **/src** directory. Docker doesn't
  70. not support relative paths for mount points inside the container.
  71. Multiple containers can use the same volume in the same time period. This is
  72. useful if two containers need access to shared data. For example, if one
  73. container writes and the other reads the data.
  74. ## Driver specific options
  75. Some volume drivers may take options to customize the volume creation. Use the
  76. **-o** or **--opt** flags to pass driver options:
  77. $ docker volume create --driver fake --opt tardis=blue --opt timey=wimey
  78. These options are passed directly to the volume driver. Options for different
  79. volume drivers may do different things (or nothing at all).
  80. The built-in **local** driver on Windows does not support any options.
  81. The built-in **local** driver on Linux accepts options similar to the linux
  82. **mount** command:
  83. $ docker volume create --driver local --opt type=tmpfs --opt device=tmpfs --opt o=size=100m,uid=1000
  84. Another example:
  85. $ docker volume create --driver local --opt type=btrfs --opt device=/dev/sda2
  86. `