github.com/portworx/docker@v1.12.1/api/client/volume/create.go (about)

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