github.com/kunnos/engine@v1.13.1/cli/command/volume/create.go (about)

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