2016-09-08 13:11:39 -04:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-10-14 16:20:13 -04:00
|
|
|
volumetypes "github.com/docker/docker/api/types/volume"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type createOptions struct {
|
|
|
|
name string
|
|
|
|
driver string
|
|
|
|
driverOpts opts.MapOpts
|
2016-10-12 19:06:34 -04:00
|
|
|
labels opts.ListOpts
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
opts := createOptions{
|
|
|
|
driverOpts: *opts.NewMapOpts(nil, nil),
|
2016-10-12 19:06:34 -04:00
|
|
|
labels: opts.NewListOpts(runconfigopts.ValidateEnv),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "create [OPTIONS] [VOLUME]",
|
|
|
|
Short: "Create a volume",
|
|
|
|
Long: createDescription,
|
|
|
|
Args: cli.RequiresMaxArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) == 1 {
|
|
|
|
if opts.name != "" {
|
|
|
|
fmt.Fprint(dockerCli.Err(), "Conflicting options: either specify --name or provide positional arg, not both\n")
|
|
|
|
return cli.StatusError{StatusCode: 1}
|
|
|
|
}
|
|
|
|
opts.name = args[0]
|
|
|
|
}
|
|
|
|
return runCreate(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.StringVarP(&opts.driver, "driver", "d", "local", "Specify volume driver name")
|
|
|
|
flags.StringVar(&opts.name, "name", "", "Specify volume name")
|
|
|
|
flags.Lookup("name").Hidden = true
|
|
|
|
flags.VarP(&opts.driverOpts, "opt", "o", "Set driver specific options")
|
2016-10-12 19:06:34 -04:00
|
|
|
flags.Var(&opts.labels, "label", "Set metadata for a volume")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCreate(dockerCli *command.DockerCli, opts createOptions) error {
|
|
|
|
client := dockerCli.Client()
|
|
|
|
|
2016-10-06 12:57:17 -04:00
|
|
|
volReq := volumetypes.VolumesCreateBody{
|
2016-09-08 13:11:39 -04:00
|
|
|
Driver: opts.driver,
|
|
|
|
DriverOpts: opts.driverOpts.GetAll(),
|
|
|
|
Name: opts.name,
|
2016-10-12 19:06:34 -04:00
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(opts.labels.GetAll()),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
vol, err := client.VolumeCreate(context.Background(), volReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", vol.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var createDescription = `
|
|
|
|
Creates a new volume that containers can consume and store data in. If a name
|
|
|
|
is not specified, Docker generates a random name. You create a volume and then
|
|
|
|
configure the container to use it, for example:
|
|
|
|
|
|
|
|
$ docker volume create hello
|
|
|
|
hello
|
|
|
|
$ docker run -d -v hello:/world busybox ls /world
|
|
|
|
|
|
|
|
The mount is created inside the container's **/src** directory. Docker doesn't
|
|
|
|
not support relative paths for mount points inside the container.
|
|
|
|
|
|
|
|
Multiple containers can use the same volume in the same time period. This is
|
|
|
|
useful if two containers need access to shared data. For example, if one
|
|
|
|
container writes and the other reads the data.
|
|
|
|
|
|
|
|
## Driver specific options
|
|
|
|
|
|
|
|
Some volume drivers may take options to customize the volume creation. Use the
|
|
|
|
**-o** or **--opt** flags to pass driver options:
|
|
|
|
|
|
|
|
$ docker volume create --driver fake --opt tardis=blue --opt timey=wimey
|
|
|
|
|
|
|
|
These options are passed directly to the volume driver. Options for different
|
|
|
|
volume drivers may do different things (or nothing at all).
|
|
|
|
|
|
|
|
The built-in **local** driver on Windows does not support any options.
|
|
|
|
|
|
|
|
The built-in **local** driver on Linux accepts options similar to the linux
|
|
|
|
**mount** command:
|
|
|
|
|
|
|
|
$ docker volume create --driver local --opt type=tmpfs --opt device=tmpfs --opt o=size=100m,uid=1000
|
|
|
|
|
|
|
|
Another example:
|
|
|
|
|
|
|
|
$ docker volume create --driver local --opt type=btrfs --opt device=/dev/sda2
|
|
|
|
|
|
|
|
`
|