2016-09-08 13:11:39 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-05-15 19:01:48 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-02-16 20:05:36 -05:00
|
|
|
"github.com/spf13/pflag"
|
2016-09-08 13:11:39 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2017-07-20 04:32:51 -04:00
|
|
|
func newCreateCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
opts := newServiceOptions()
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "create [OPTIONS] IMAGE [COMMAND] [ARG...]",
|
|
|
|
Short: "Create a new service",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.image = args[0]
|
|
|
|
if len(args) > 1 {
|
|
|
|
opts.args = args[1:]
|
|
|
|
}
|
2017-02-16 20:05:36 -05:00
|
|
|
return runCreate(dockerCli, cmd.Flags(), opts)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.StringVar(&opts.mode, flagMode, "replicated", "Service mode (replicated or global)")
|
2016-09-28 06:34:31 -04:00
|
|
|
flags.StringVar(&opts.name, flagName, "", "Service name")
|
|
|
|
|
2017-03-30 21:35:04 -04:00
|
|
|
addServiceFlags(flags, opts, buildServiceDefaultFlagMapping())
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
flags.VarP(&opts.labels, flagLabel, "l", "Service labels")
|
|
|
|
flags.Var(&opts.containerLabels, flagContainerLabel, "Container labels")
|
|
|
|
flags.VarP(&opts.env, flagEnv, "e", "Set environment variables")
|
2016-07-20 02:58:32 -04:00
|
|
|
flags.Var(&opts.envFile, flagEnvFile, "Read in a file of environment variables")
|
2016-10-24 23:26:54 -04:00
|
|
|
flags.Var(&opts.mounts, flagMount, "Attach a filesystem mount to the service")
|
2016-11-08 10:06:07 -05:00
|
|
|
flags.Var(&opts.constraints, flagConstraint, "Placement constraints")
|
2017-01-19 18:27:37 -05:00
|
|
|
flags.Var(&opts.placementPrefs, flagPlacementPref, "Add a placement preference")
|
2017-03-13 21:31:48 -04:00
|
|
|
flags.SetAnnotation(flagPlacementPref, "version", []string{"1.28"})
|
2016-11-08 10:06:07 -05:00
|
|
|
flags.Var(&opts.networks, flagNetwork, "Network attachments")
|
2016-11-01 22:28:32 -04:00
|
|
|
flags.Var(&opts.secrets, flagSecret, "Specify secrets to expose to the service")
|
2017-01-16 11:57:26 -05:00
|
|
|
flags.SetAnnotation(flagSecret, "version", []string{"1.25"})
|
2017-05-08 13:36:04 -04:00
|
|
|
flags.Var(&opts.configs, flagConfig, "Specify configurations to expose to the service")
|
|
|
|
flags.SetAnnotation(flagConfig, "version", []string{"1.30"})
|
2016-11-10 15:13:26 -05:00
|
|
|
flags.VarP(&opts.endpoint.publishPorts, flagPublish, "p", "Publish a port as a node port")
|
2016-11-08 10:06:07 -05:00
|
|
|
flags.Var(&opts.groups, flagGroup, "Set one or more supplementary user groups for the container")
|
2017-01-16 11:57:26 -05:00
|
|
|
flags.SetAnnotation(flagGroup, "version", []string{"1.25"})
|
2016-10-19 20:07:44 -04:00
|
|
|
flags.Var(&opts.dns, flagDNS, "Set custom DNS servers")
|
2017-01-16 11:57:26 -05:00
|
|
|
flags.SetAnnotation(flagDNS, "version", []string{"1.25"})
|
2016-11-08 21:29:10 -05:00
|
|
|
flags.Var(&opts.dnsOption, flagDNSOption, "Set DNS options")
|
2017-01-16 11:57:26 -05:00
|
|
|
flags.SetAnnotation(flagDNSOption, "version", []string{"1.25"})
|
2016-10-19 20:07:44 -04:00
|
|
|
flags.Var(&opts.dnsSearch, flagDNSSearch, "Set custom DNS search domains")
|
2017-01-16 11:57:26 -05:00
|
|
|
flags.SetAnnotation(flagDNSSearch, "version", []string{"1.25"})
|
2016-11-03 11:05:00 -04:00
|
|
|
flags.Var(&opts.hosts, flagHost, "Set one or more custom host-to-IP mappings (host:ip)")
|
2017-01-16 11:57:26 -05:00
|
|
|
flags.SetAnnotation(flagHost, "version", []string{"1.25"})
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
flags.SetInterspersed(false)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-07-20 04:32:51 -04:00
|
|
|
func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, opts *serviceOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
apiClient := dockerCli.Client()
|
|
|
|
createOpts := types.ServiceCreateOptions{}
|
|
|
|
|
2017-03-23 20:51:57 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-03-30 21:35:04 -04:00
|
|
|
service, err := opts.ToService(ctx, apiClient, flags)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-11 09:56:25 -05:00
|
|
|
specifiedSecrets := opts.secrets.Value()
|
|
|
|
if len(specifiedSecrets) > 0 {
|
|
|
|
// parse and validate secrets
|
2017-01-10 17:40:53 -05:00
|
|
|
secrets, err := ParseSecrets(apiClient, specifiedSecrets)
|
2016-11-11 09:56:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
service.TaskTemplate.ContainerSpec.Secrets = secrets
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
2016-11-11 09:56:25 -05:00
|
|
|
|
2017-05-08 13:36:04 -04:00
|
|
|
specifiedConfigs := opts.configs.Value()
|
|
|
|
if len(specifiedConfigs) > 0 {
|
|
|
|
// parse and validate configs
|
|
|
|
configs, err := ParseConfigs(apiClient, specifiedConfigs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
service.TaskTemplate.ContainerSpec.Configs = configs
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2017-05-11 05:07:35 -04:00
|
|
|
if err := resolveServiceImageDigestContentTrust(dockerCli, &service); err != nil {
|
2016-12-05 20:02:26 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
// only send auth if flag was set
|
|
|
|
if opts.registryAuth {
|
|
|
|
// Retrieve encoded auth token from the image reference
|
2016-09-09 15:38:00 -04:00
|
|
|
encodedAuth, err := command.RetrieveAuthTokenFromImage(ctx, dockerCli, opts.image)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
createOpts.EncodedRegistryAuth = encodedAuth
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:01:48 -04:00
|
|
|
// query registry if flag disabling it was not set
|
|
|
|
if !opts.noResolveImage && versions.GreaterThanOrEqualTo(apiClient.ClientVersion(), "1.30") {
|
|
|
|
createOpts.QueryRegistry = true
|
|
|
|
}
|
2017-05-11 05:07:35 -04:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
response, err := apiClient.ServiceCreate(ctx, service, createOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-14 21:08:24 -05:00
|
|
|
for _, warning := range response.Warnings {
|
|
|
|
fmt.Fprintln(dockerCli.Err(), warning)
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", response.ID)
|
2017-02-16 20:05:36 -05:00
|
|
|
|
|
|
|
if opts.detach {
|
2017-06-26 21:19:36 -04:00
|
|
|
warnDetachDefault(dockerCli.Err(), apiClient.ClientVersion(), flags, "created")
|
2017-02-16 20:05:36 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:19:36 -04:00
|
|
|
return waitOnService(ctx, dockerCli, response.ID, opts.quiet)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|