2016-04-19 12:59:48 -04:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-05-16 17:20:29 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-04-19 12:59:48 -04:00
|
|
|
"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
|
|
|
|
labels []string
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-05-16 17:20:29 -04:00
|
|
|
opts := createOptions{
|
|
|
|
driverOpts: *opts.NewMapOpts(nil, nil),
|
|
|
|
}
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-06-14 18:42:30 -04:00
|
|
|
Use: "create [OPTIONS] [VOLUME]",
|
2016-04-19 12:59:48 -04:00
|
|
|
Short: "Create a volume",
|
2016-06-20 16:33:53 -04:00
|
|
|
Long: createDescription,
|
2016-06-14 18:42:30 -04:00
|
|
|
Args: cli.RequiresMaxArgs(1),
|
2016-04-19 12:59:48 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-06-14 18:42:30 -04:00
|
|
|
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]
|
|
|
|
}
|
2016-04-19 12:59:48 -04:00
|
|
|
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")
|
2016-06-14 18:42:30 -04:00
|
|
|
flags.Lookup("name").Hidden = true
|
2016-04-19 12:59:48 -04:00
|
|
|
flags.VarP(&opts.driverOpts, "opt", "o", "Set driver specific options")
|
|
|
|
flags.StringSliceVar(&opts.labels, "label", []string{}, "Set metadata for a volume")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runCreate(dockerCli *command.DockerCli, opts createOptions) error {
|
2016-04-19 12:59:48 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
|
|
|
|
volReq := types.VolumeCreateRequest{
|
|
|
|
Driver: opts.driver,
|
|
|
|
DriverOpts: opts.driverOpts.GetAll(),
|
|
|
|
Name: opts.name,
|
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(opts.labels),
|
|
|
|
}
|
|
|
|
|
|
|
|
vol, err := client.VolumeCreate(context.Background(), volReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", vol.Name)
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-20 16:33:53 -04:00
|
|
|
|
|
|
|
var createDescription = `
|
2016-06-22 10:12:18 -04:00
|
|
|
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:
|
2016-06-20 16:33:53 -04:00
|
|
|
|
2016-06-14 18:42:30 -04:00
|
|
|
$ docker volume create hello
|
2016-06-20 16:33:53 -04:00
|
|
|
hello
|
|
|
|
$ docker run -d -v hello:/world busybox ls /world
|
|
|
|
|
2016-06-22 10:12:18 -04:00
|
|
|
The mount is created inside the container's **/src** directory. Docker doesn't
|
|
|
|
not support relative paths for mount points inside the container.
|
2016-06-20 16:33:53 -04:00
|
|
|
|
2016-06-22 10:12:18 -04:00
|
|
|
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.
|
2016-06-20 16:33:53 -04:00
|
|
|
|
|
|
|
## Driver specific options
|
|
|
|
|
2016-06-22 10:12:18 -04:00
|
|
|
Some volume drivers may take options to customize the volume creation. Use the
|
|
|
|
**-o** or **--opt** flags to pass driver options:
|
2016-06-20 16:33:53 -04:00
|
|
|
|
|
|
|
$ docker volume create --driver fake --opt tardis=blue --opt timey=wimey
|
|
|
|
|
2016-06-22 10:12:18 -04:00
|
|
|
These options are passed directly to the volume driver. Options for different
|
|
|
|
volume drivers may do different things (or nothing at all).
|
2016-06-20 16:33:53 -04:00
|
|
|
|
|
|
|
The built-in **local** driver on Windows does not support any options.
|
|
|
|
|
2016-06-22 10:12:18 -04:00
|
|
|
The built-in **local** driver on Linux accepts options similar to the linux
|
|
|
|
**mount** command:
|
2016-06-20 16:33:53 -04:00
|
|
|
|
|
|
|
$ 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
|
|
|
|
|
|
|
|
`
|