From 88be16cb5bebac3451733ef4a0b003eefb1a4260 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Mon, 3 Apr 2023 17:08:36 +0900 Subject: [PATCH] cli/command/container: implement `docker run --annotation` For moby/moby PR 45025 (Docker v24, API v1.43). `docker run --annotation foo=bar` is similar to `podman run --annotation foo=bar`, however, unlike Podman, Docker implementation also accepts an annotation with an empty value. (`docker run --annotation foo`) Signed-off-by: Akihiro Suda --- cli/command/container/opts.go | 7 +++++++ contrib/completion/bash/docker | 1 + contrib/completion/fish/docker.fish | 2 ++ contrib/completion/zsh/_docker | 1 + docs/reference/commandline/container_create.md | 1 + docs/reference/commandline/container_run.md | 1 + docs/reference/commandline/create.md | 1 + docs/reference/commandline/run.md | 1 + man/docker-run.1.md | 6 ++++++ 9 files changed, 21 insertions(+) diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index e4db20ee3e..a68bbe4833 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -123,6 +123,7 @@ type containerOptions struct { runtime string autoRemove bool init bool + annotations *opts.MapOpts Image string Args []string @@ -163,6 +164,7 @@ func addFlags(flags *pflag.FlagSet) *containerOptions { ulimits: opts.NewUlimitOpt(nil), volumes: opts.NewListOpts(nil), volumesFrom: opts.NewListOpts(nil), + annotations: opts.NewMapOpts(nil, nil), } // General purpose flags @@ -297,6 +299,10 @@ func addFlags(flags *pflag.FlagSet) *containerOptions { flags.BoolVar(&copts.init, "init", false, "Run an init inside the container that forwards signals and reaps processes") flags.SetAnnotation("init", "version", []string{"1.25"}) + + flags.Var(copts.annotations, "annotation", "Add an annotation to the container (passed through to the OCI runtime)") + flags.SetAnnotation("annotation", "version", []string{"1.43"}) + return copts } @@ -654,6 +660,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con Mounts: mounts, MaskedPaths: maskedPaths, ReadonlyPaths: readonlyPaths, + Annotations: copts.annotations.GetAll(), } if copts.autoRemove && !hostConfig.RestartPolicy.IsNone() { diff --git a/contrib/completion/bash/docker b/contrib/completion/bash/docker index 1adfb9c823..2d0c400f0c 100644 --- a/contrib/completion/bash/docker +++ b/contrib/completion/bash/docker @@ -1888,6 +1888,7 @@ _docker_container_run() { _docker_container_run_and_create() { local options_with_args=" --add-host + --annotation --attach -a --blkio-weight --blkio-weight-device diff --git a/contrib/completion/fish/docker.fish b/contrib/completion/fish/docker.fish index a0df1b3473..508a0c8b87 100644 --- a/contrib/completion/fish/docker.fish +++ b/contrib/completion/fish/docker.fish @@ -178,6 +178,7 @@ complete -c docker -A -f -n '__fish_seen_subcommand_from cp' -l help -d 'Print u # create complete -c docker -f -n '__fish_docker_no_subcommand' -a create -d 'Create a new container' complete -c docker -A -f -n '__fish_seen_subcommand_from create' -l add-host -d 'Add a custom host-to-IP mapping (host:ip)' +complete -c docker -A -f -n '__fish_seen_subcommand_from create' -l annotation -d 'Add an annotation to the container (passed through to the OCI runtime)' complete -c docker -A -f -n '__fish_seen_subcommand_from create' -s a -l attach -d 'Attach to STDIN, STDOUT or STDERR.' complete -c docker -A -f -n '__fish_seen_subcommand_from create' -l blkio-weight -d 'Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)' complete -c docker -A -f -n '__fish_seen_subcommand_from create' -l blkio-weight-device -d 'Block IO weight (relative device weight)' @@ -453,6 +454,7 @@ complete -c docker -A -f -n '__fish_seen_subcommand_from rmi' -a '(__fish_print_ # run complete -c docker -f -n '__fish_docker_no_subcommand' -a run -d 'Create and run a new container from an image' +complete -c docker -A -f -n '__fish_seen_subcommand_from run' -l annotation -d 'Add an annotation to the container (passed through to the OCI runtime)' complete -c docker -A -f -n '__fish_seen_subcommand_from run' -s a -l attach -d 'Attach to STDIN, STDOUT or STDERR.' complete -c docker -A -f -n '__fish_seen_subcommand_from run' -l add-host -d 'Add a custom host-to-IP mapping (host:ip)' complete -c docker -A -f -n '__fish_seen_subcommand_from run' -s c -l cpu-shares -d 'CPU shares (relative weight)' diff --git a/contrib/completion/zsh/_docker b/contrib/completion/zsh/_docker index 15030eb614..7926bb078a 100644 --- a/contrib/completion/zsh/_docker +++ b/contrib/completion/zsh/_docker @@ -602,6 +602,7 @@ __docker_container_subcommand() { opts_create_run=( "($help -a --attach)"{-a=,--attach=}"[Attach to stdin, stdout or stderr]:device:(STDIN STDOUT STDERR)" "($help)*--add-host=[Add a custom host-to-IP mapping]:host\:ip mapping: " + "($help)*--annotation=[Add an annotation to the container (passed through to the OCI runtime)]:annotations: " "($help)*--blkio-weight-device=[Block IO (relative device weight)]:device:Block IO weight: " "($help)*--cap-add=[Add Linux capabilities]:capability: " "($help)*--cap-drop=[Drop Linux capabilities]:capability: " diff --git a/docs/reference/commandline/container_create.md b/docs/reference/commandline/container_create.md index ddd74b15cc..ff6095bab8 100644 --- a/docs/reference/commandline/container_create.md +++ b/docs/reference/commandline/container_create.md @@ -12,6 +12,7 @@ Create a new container | Name | Type | Default | Description | |:--------------------------|:--------------|:----------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `--add-host` | `list` | | Add a custom host-to-IP mapping (host:ip) | +| `--annotation` | `map` | `map[]` | Add an annotation to the container (passed through to the OCI runtime) | | `-a`, `--attach` | `list` | | Attach to STDIN, STDOUT or STDERR | | `--blkio-weight` | `uint16` | `0` | Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) | | `--blkio-weight-device` | `list` | | Block IO weight (relative device weight) | diff --git a/docs/reference/commandline/container_run.md b/docs/reference/commandline/container_run.md index 63ec20b88d..f38f1cf5b6 100644 --- a/docs/reference/commandline/container_run.md +++ b/docs/reference/commandline/container_run.md @@ -12,6 +12,7 @@ Create and run a new container from an image | Name | Type | Default | Description | |:--------------------------|:--------------|:----------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `--add-host` | `list` | | Add a custom host-to-IP mapping (host:ip) | +| `--annotation` | `map` | `map[]` | Add an annotation to the container (passed through to the OCI runtime) | | `-a`, `--attach` | `list` | | Attach to STDIN, STDOUT or STDERR | | `--blkio-weight` | `uint16` | `0` | Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) | | `--blkio-weight-device` | `list` | | Block IO weight (relative device weight) | diff --git a/docs/reference/commandline/create.md b/docs/reference/commandline/create.md index c85786328b..208d2a8455 100644 --- a/docs/reference/commandline/create.md +++ b/docs/reference/commandline/create.md @@ -12,6 +12,7 @@ Create a new container | Name | Type | Default | Description | |:--------------------------|:--------------|:----------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `--add-host` | `list` | | Add a custom host-to-IP mapping (host:ip) | +| `--annotation` | `map` | `map[]` | Add an annotation to the container (passed through to the OCI runtime) | | `-a`, `--attach` | `list` | | Attach to STDIN, STDOUT or STDERR | | `--blkio-weight` | `uint16` | `0` | Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) | | `--blkio-weight-device` | `list` | | Block IO weight (relative device weight) | diff --git a/docs/reference/commandline/run.md b/docs/reference/commandline/run.md index 49a7497e2d..060c512878 100644 --- a/docs/reference/commandline/run.md +++ b/docs/reference/commandline/run.md @@ -12,6 +12,7 @@ Create and run a new container from an image | Name | Type | Default | Description | |:----------------------------------------------|:--------------|:----------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [`--add-host`](#add-host) | `list` | | Add a custom host-to-IP mapping (host:ip) | +| `--annotation` | `map` | `map[]` | Add an annotation to the container (passed through to the OCI runtime) | | [`-a`](#attach), [`--attach`](#attach) | `list` | | Attach to STDIN, STDOUT or STDERR | | `--blkio-weight` | `uint16` | `0` | Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) | | `--blkio-weight-device` | `list` | | Block IO weight (relative device weight) | diff --git a/man/docker-run.1.md b/man/docker-run.1.md index d345a89907..13d42984f0 100644 --- a/man/docker-run.1.md +++ b/man/docker-run.1.md @@ -7,6 +7,7 @@ docker-run - Create and run a new container from an image **docker run** [**-a**|**--attach**[=*[]*]] [**--add-host**[=*[]*]] +[**--annotation**[=*[]*]] [**--blkio-weight**[=*[BLKIO-WEIGHT]*]] [**--blkio-weight-device**[=*[]*]] [**--cpu-shares**[=*0*]] @@ -125,6 +126,11 @@ each of stdin, stdout, and stderr. Add a line to /etc/hosts. The format is hostname:ip. The **--add-host** option can be set multiple times. +**--annotation**=[] + Add an annotation to the container (passed through to the OCI runtime). + + The annotations are provided to the OCI runtime. + **--blkio-weight**=*0* Block IO weight (relative weight) accepts a weight value between 10 and 1000.