2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2017-03-27 21:21:59 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type updateOptions struct {
|
2016-06-07 15:05:43 -04:00
|
|
|
blkioWeight uint16
|
|
|
|
cpuPeriod int64
|
|
|
|
cpuQuota int64
|
|
|
|
cpuRealtimePeriod int64
|
|
|
|
cpuRealtimeRuntime int64
|
|
|
|
cpusetCpus string
|
|
|
|
cpusetMems string
|
|
|
|
cpuShares int64
|
2017-03-06 16:01:04 -05:00
|
|
|
memory opts.MemBytes
|
|
|
|
memoryReservation opts.MemBytes
|
|
|
|
memorySwap opts.MemSwapBytes
|
|
|
|
kernelMemory opts.MemBytes
|
2016-06-07 15:05:43 -04:00
|
|
|
restartPolicy string
|
2017-06-12 02:22:11 -04:00
|
|
|
pidsLimit int64
|
2017-02-18 01:04:37 -05:00
|
|
|
cpus opts.NanoCPUs
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
nFlag int
|
|
|
|
|
|
|
|
containers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUpdateCommand creates a new cobra.Command for `docker update`
|
2017-10-11 12:18:27 -04:00
|
|
|
func NewUpdateCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
var options updateOptions
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "update [OPTIONS] CONTAINER [CONTAINER...]",
|
|
|
|
Short: "Update configuration of one or more containers",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.containers = args
|
|
|
|
options.nFlag = cmd.Flags().NFlag()
|
2023-09-09 18:27:44 -04:00
|
|
|
return runUpdate(cmd.Context(), dockerCli, &options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"aliases": "docker container update, docker update",
|
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.ContainerNames(dockerCli, true),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2023-01-03 05:12:24 -05:00
|
|
|
flags.Uint16Var(&options.blkioWeight, "blkio-weight", 0, `Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)`)
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.Int64Var(&options.cpuPeriod, "cpu-period", 0, "Limit CPU CFS (Completely Fair Scheduler) period")
|
|
|
|
flags.Int64Var(&options.cpuQuota, "cpu-quota", 0, "Limit CPU CFS (Completely Fair Scheduler) quota")
|
|
|
|
flags.Int64Var(&options.cpuRealtimePeriod, "cpu-rt-period", 0, "Limit the CPU real-time period in microseconds")
|
2017-01-16 11:57:26 -05:00
|
|
|
flags.SetAnnotation("cpu-rt-period", "version", []string{"1.25"})
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.Int64Var(&options.cpuRealtimeRuntime, "cpu-rt-runtime", 0, "Limit the CPU real-time runtime in microseconds")
|
2017-01-16 11:57:26 -05:00
|
|
|
flags.SetAnnotation("cpu-rt-runtime", "version", []string{"1.25"})
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.StringVar(&options.cpusetCpus, "cpuset-cpus", "", "CPUs in which to allow execution (0-3, 0,1)")
|
|
|
|
flags.StringVar(&options.cpusetMems, "cpuset-mems", "", "MEMs in which to allow execution (0-3, 0,1)")
|
|
|
|
flags.Int64VarP(&options.cpuShares, "cpu-shares", "c", 0, "CPU shares (relative weight)")
|
|
|
|
flags.VarP(&options.memory, "memory", "m", "Memory limit")
|
|
|
|
flags.Var(&options.memoryReservation, "memory-reservation", "Memory soft limit")
|
2023-01-03 05:12:24 -05:00
|
|
|
flags.Var(&options.memorySwap, "memory-swap", `Swap limit equal to memory plus swap: -1 to enable unlimited swap`)
|
2022-04-07 08:36:11 -04:00
|
|
|
flags.Var(&options.kernelMemory, "kernel-memory", "Kernel memory limit (deprecated)")
|
|
|
|
// --kernel-memory is deprecated on API v1.42 and up, but our current annotations
|
|
|
|
// do not support only showing on < API-version. This option is no longer supported
|
|
|
|
// by runc, so hiding it unconditionally.
|
|
|
|
flags.SetAnnotation("kernel-memory", "deprecated", nil)
|
|
|
|
flags.MarkHidden("kernel-memory")
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.StringVar(&options.restartPolicy, "restart", "", "Restart policy to apply when a container exits")
|
2023-01-03 05:12:24 -05:00
|
|
|
flags.Int64Var(&options.pidsLimit, "pids-limit", 0, `Tune container pids limit (set -1 for unlimited)`)
|
2017-06-12 02:22:11 -04:00
|
|
|
flags.SetAnnotation("pids-limit", "version", []string{"1.40"})
|
2017-05-15 08:45:19 -04:00
|
|
|
|
|
|
|
flags.Var(&options.cpus, "cpus", "Number of CPUs")
|
2017-02-18 01:04:37 -05:00
|
|
|
flags.SetAnnotation("cpus", "version", []string{"1.29"})
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runUpdate(ctx context.Context, dockerCli command.Cli, options *updateOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
var err error
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
if options.nFlag == 0 {
|
2017-05-02 15:35:25 -04:00
|
|
|
return errors.New("you must provide one or more flags when using this command")
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var restartPolicy containertypes.RestartPolicy
|
2017-05-15 08:45:19 -04:00
|
|
|
if options.restartPolicy != "" {
|
2017-06-05 18:23:21 -04:00
|
|
|
restartPolicy, err = opts.ParseRestartPolicy(options.restartPolicy)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resources := containertypes.Resources{
|
2017-05-15 08:45:19 -04:00
|
|
|
BlkioWeight: options.blkioWeight,
|
|
|
|
CpusetCpus: options.cpusetCpus,
|
|
|
|
CpusetMems: options.cpusetMems,
|
|
|
|
CPUShares: options.cpuShares,
|
|
|
|
Memory: options.memory.Value(),
|
|
|
|
MemoryReservation: options.memoryReservation.Value(),
|
|
|
|
MemorySwap: options.memorySwap.Value(),
|
|
|
|
KernelMemory: options.kernelMemory.Value(),
|
|
|
|
CPUPeriod: options.cpuPeriod,
|
|
|
|
CPUQuota: options.cpuQuota,
|
|
|
|
CPURealtimePeriod: options.cpuRealtimePeriod,
|
|
|
|
CPURealtimeRuntime: options.cpuRealtimeRuntime,
|
|
|
|
NanoCPUs: options.cpus.Value(),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-06-12 02:22:11 -04:00
|
|
|
if options.pidsLimit != 0 {
|
|
|
|
resources.PidsLimit = &options.pidsLimit
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
updateConfig := containertypes.UpdateConfig{
|
|
|
|
Resources: resources,
|
|
|
|
RestartPolicy: restartPolicy,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
warns []string
|
|
|
|
errs []string
|
|
|
|
)
|
2024-07-03 10:14:40 -04:00
|
|
|
for _, ctr := range options.containers {
|
|
|
|
r, err := dockerCli.Client().ContainerUpdate(ctx, ctr, updateConfig)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err.Error())
|
|
|
|
} else {
|
2024-07-03 10:14:40 -04:00
|
|
|
_, _ = fmt.Fprintln(dockerCli.Out(), ctr)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
warns = append(warns, r.Warnings...)
|
|
|
|
}
|
|
|
|
if len(warns) > 0 {
|
2024-07-03 10:14:40 -04:00
|
|
|
_, _ = fmt.Fprintln(dockerCli.Out(), strings.Join(warns, "\n"))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
2016-12-24 19:05:37 -05:00
|
|
|
return errors.New(strings.Join(errs, "\n"))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|