create.go 2.9 KB

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