github.com/olljanat/moby@v1.13.1/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  	flags.StringVar(&opts.name, flagName, "", "Service name")
    31  
    32  	addServiceFlags(cmd, opts)
    33  
    34  	flags.VarP(&opts.labels, flagLabel, "l", "Service labels")
    35  	flags.Var(&opts.containerLabels, flagContainerLabel, "Container labels")
    36  	flags.VarP(&opts.env, flagEnv, "e", "Set environment variables")
    37  	flags.Var(&opts.envFile, flagEnvFile, "Read in a file of environment variables")
    38  	flags.Var(&opts.mounts, flagMount, "Attach a filesystem mount to the service")
    39  	flags.Var(&opts.constraints, flagConstraint, "Placement constraints")
    40  	flags.Var(&opts.networks, flagNetwork, "Network attachments")
    41  	flags.Var(&opts.secrets, flagSecret, "Specify secrets to expose to the service")
    42  	flags.VarP(&opts.endpoint.publishPorts, flagPublish, "p", "Publish a port as a node port")
    43  	flags.Var(&opts.groups, flagGroup, "Set one or more supplementary user groups for the container")
    44  	flags.Var(&opts.dns, flagDNS, "Set custom DNS servers")
    45  	flags.Var(&opts.dnsOption, flagDNSOption, "Set DNS options")
    46  	flags.Var(&opts.dnsSearch, flagDNSSearch, "Set custom DNS search domains")
    47  	flags.Var(&opts.hosts, flagHost, "Set one or more custom host-to-IP mappings (host:ip)")
    48  
    49  	flags.SetInterspersed(false)
    50  	return cmd
    51  }
    52  
    53  func runCreate(dockerCli *command.DockerCli, opts *serviceOptions) error {
    54  	apiClient := dockerCli.Client()
    55  	createOpts := types.ServiceCreateOptions{}
    56  
    57  	service, err := opts.ToService()
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	specifiedSecrets := opts.secrets.Value()
    63  	if len(specifiedSecrets) > 0 {
    64  		// parse and validate secrets
    65  		secrets, err := ParseSecrets(apiClient, specifiedSecrets)
    66  		if err != nil {
    67  			return err
    68  		}
    69  		service.TaskTemplate.ContainerSpec.Secrets = secrets
    70  
    71  	}
    72  
    73  	ctx := context.Background()
    74  
    75  	if err := resolveServiceImageDigest(dockerCli, &service); err != nil {
    76  		return err
    77  	}
    78  
    79  	// only send auth if flag was set
    80  	if opts.registryAuth {
    81  		// Retrieve encoded auth token from the image reference
    82  		encodedAuth, err := command.RetrieveAuthTokenFromImage(ctx, dockerCli, opts.image)
    83  		if err != nil {
    84  			return err
    85  		}
    86  		createOpts.EncodedRegistryAuth = encodedAuth
    87  	}
    88  
    89  	response, err := apiClient.ServiceCreate(ctx, service, createOpts)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	for _, warning := range response.Warnings {
    95  		fmt.Fprintln(dockerCli.Err(), warning)
    96  	}
    97  
    98  	fmt.Fprintf(dockerCli.Out(), "%s\n", response.ID)
    99  	return nil
   100  }