create.go 2.5 KB

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