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