github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/command/service/create.go (about) 1 package service 2 3 import ( 4 "fmt" 5 6 "github.com/docker/docker/api/types" 7 "github.com/docker/docker/cli" 8 "github.com/docker/docker/cli/command" 9 "github.com/spf13/cobra" 10 "golang.org/x/net/context" 11 ) 12 13 func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command { 14 opts := newServiceOptions() 15 16 cmd := &cobra.Command{ 17 Use: "create [OPTIONS] IMAGE [COMMAND] [ARG...]", 18 Short: "Create a new service", 19 Args: cli.RequiresMinArgs(1), 20 RunE: func(cmd *cobra.Command, args []string) error { 21 opts.image = args[0] 22 if len(args) > 1 { 23 opts.args = args[1:] 24 } 25 return runCreate(dockerCli, opts) 26 }, 27 } 28 flags := cmd.Flags() 29 flags.StringVar(&opts.mode, flagMode, "replicated", "Service mode (replicated or global)") 30 addServiceFlags(cmd, opts) 31 32 flags.VarP(&opts.labels, flagLabel, "l", "Service labels") 33 flags.Var(&opts.containerLabels, flagContainerLabel, "Container labels") 34 flags.VarP(&opts.env, flagEnv, "e", "Set environment variables") 35 flags.Var(&opts.mounts, flagMount, "Attach a mount to the service") 36 flags.StringSliceVar(&opts.constraints, flagConstraint, []string{}, "Placement constraints") 37 flags.StringSliceVar(&opts.networks, flagNetwork, []string{}, "Network attachments") 38 flags.VarP(&opts.endpoint.ports, flagPublish, "p", "Publish a port as a node port") 39 40 flags.SetInterspersed(false) 41 return cmd 42 } 43 44 func runCreate(dockerCli *command.DockerCli, opts *serviceOptions) error { 45 apiClient := dockerCli.Client() 46 createOpts := types.ServiceCreateOptions{} 47 48 service, err := opts.ToService() 49 if err != nil { 50 return err 51 } 52 53 ctx := context.Background() 54 55 // only send auth if flag was set 56 if opts.registryAuth { 57 // Retrieve encoded auth token from the image reference 58 encodedAuth, err := command.RetrieveAuthTokenFromImage(ctx, dockerCli, opts.image) 59 if err != nil { 60 return err 61 } 62 createOpts.EncodedRegistryAuth = encodedAuth 63 } 64 65 response, err := apiClient.ServiceCreate(ctx, service, createOpts) 66 if err != nil { 67 return err 68 } 69 70 fmt.Fprintf(dockerCli.Out(), "%s\n", response.ID) 71 return nil 72 }