create.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.Var(&opts.constraints, flagConstraint, "Placement constraints")
  35. flags.Var(&opts.networks, flagNetwork, "Network attachments")
  36. flags.Var(&opts.secrets, flagSecret, "Specify secrets to expose to the service")
  37. flags.VarP(&opts.endpoint.ports, flagPublish, "p", "Publish a port as a node port")
  38. flags.Var(&opts.groups, flagGroup, "Set one or more supplementary user groups for the container")
  39. flags.Var(&opts.dns, flagDNS, "Set custom DNS servers")
  40. flags.Var(&opts.dnsOption, flagDNSOption, "Set DNS options")
  41. flags.Var(&opts.dnsSearch, flagDNSSearch, "Set custom DNS search domains")
  42. flags.Var(&opts.hosts, flagHost, "Set one or more custom host-to-IP mappings (host:ip)")
  43. flags.SetInterspersed(false)
  44. return cmd
  45. }
  46. func runCreate(dockerCli *command.DockerCli, opts *serviceOptions) error {
  47. apiClient := dockerCli.Client()
  48. createOpts := types.ServiceCreateOptions{}
  49. service, err := opts.ToService()
  50. if err != nil {
  51. return err
  52. }
  53. // parse and validate secrets
  54. secrets, err := parseSecrets(apiClient, opts.secrets.Value())
  55. if err != nil {
  56. return err
  57. }
  58. service.TaskTemplate.ContainerSpec.Secrets = secrets
  59. ctx := context.Background()
  60. // only send auth if flag was set
  61. if opts.registryAuth {
  62. // Retrieve encoded auth token from the image reference
  63. encodedAuth, err := command.RetrieveAuthTokenFromImage(ctx, dockerCli, opts.image)
  64. if err != nil {
  65. return err
  66. }
  67. createOpts.EncodedRegistryAuth = encodedAuth
  68. }
  69. response, err := apiClient.ServiceCreate(ctx, service, createOpts)
  70. if err != nil {
  71. return err
  72. }
  73. fmt.Fprintf(dockerCli.Out(), "%s\n", response.ID)
  74. return nil
  75. }