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
|
|
|
|
Use non-detached mode as default for service commands
Commit 330a0035334871d92207b583c1c36d52a244753f added a `--detach=false` option
to various service-related commands, with the intent to make this the default in
a future version (17.09).
This patch changes the default to use "interactive" (non-detached), allowing
users to override this by setting the `--detach` option.
To prevent problems when connecting to older daemon versions (17.05 and below,
see commit db60f255617fd90cb093813dcdfe7eec840eeff8), the detach option is
ignored for those versions, and detach is always true.
Before this change, a warning was printed to announce the upcoming default:
$ docker service create nginx:alpine
saxiyn3pe559d753730zr0xer
Since --detach=false was not specified, tasks will be created in the background.
In a future release, --detach=false will become the default.
After this change, no warning is printed, but `--detach` is disabled;
$ docker service create nginx:alpine
y9jujwzozi0hwgj5yaadzliq6
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
Setting the `--detach` flag makes the cli use the pre-17.06 behavior:
$ docker service create --detach nginx:alpine
280hjnzy0wzje5o56gr22a46n
Running against a 17.03 daemon, without specifying the `--detach` flag;
$ docker service create nginx:alpine
kqheg7ogj0kszoa34g4p73i8q
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-09-12 08:35:19 -04:00
|
|
|
if opts.detach || versions.LessThan(apiClient.ClientVersion(), "1.29") {
|
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
|
|
|
}
|