Skip to content

Commit 88be16c

Browse files
committed
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 <akihiro.suda.cz@hco.ntt.co.jp>
1 parent 60d0659 commit 88be16c

9 files changed

Lines changed: 21 additions & 0 deletions

File tree

cli/command/container/opts.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ type containerOptions struct {
123123
runtime string
124124
autoRemove bool
125125
init bool
126+
annotations *opts.MapOpts
126127

127128
Image string
128129
Args []string
@@ -163,6 +164,7 @@ func addFlags(flags *pflag.FlagSet) *containerOptions {
163164
ulimits: opts.NewUlimitOpt(nil),
164165
volumes: opts.NewListOpts(nil),
165166
volumesFrom: opts.NewListOpts(nil),
167+
annotations: opts.NewMapOpts(nil, nil),
166168
}
167169

168170
// General purpose flags
@@ -297,6 +299,10 @@ func addFlags(flags *pflag.FlagSet) *containerOptions {
297299

298300
flags.BoolVar(&copts.init, "init", false, "Run an init inside the container that forwards signals and reaps processes")
299301
flags.SetAnnotation("init", "version", []string{"1.25"})
302+
303+
flags.Var(copts.annotations, "annotation", "Add an annotation to the container (passed through to the OCI runtime)")
304+
flags.SetAnnotation("annotation", "version", []string{"1.43"})
305+
300306
return copts
301307
}
302308

@@ -654,6 +660,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
654660
Mounts: mounts,
655661
MaskedPaths: maskedPaths,
656662
ReadonlyPaths: readonlyPaths,
663+
Annotations: copts.annotations.GetAll(),
657664
}
658665

659666
if copts.autoRemove && !hostConfig.RestartPolicy.IsNone() {

contrib/completion/bash/docker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,7 @@ _docker_container_run() {
18881888
_docker_container_run_and_create() {
18891889
local options_with_args="
18901890
--add-host
1891+
--annotation
18911892
--attach -a
18921893
--blkio-weight
18931894
--blkio-weight-device

contrib/completion/fish/docker.fish

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ complete -c docker -A -f -n '__fish_seen_subcommand_from cp' -l help -d 'Print u
178178
# create
179179
complete -c docker -f -n '__fish_docker_no_subcommand' -a create -d 'Create a new container'
180180
complete -c docker -A -f -n '__fish_seen_subcommand_from create' -l add-host -d 'Add a custom host-to-IP mapping (host:ip)'
181+
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)'
181182
complete -c docker -A -f -n '__fish_seen_subcommand_from create' -s a -l attach -d 'Attach to STDIN, STDOUT or STDERR.'
182183
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)'
183184
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_
453454

454455
# run
455456
complete -c docker -f -n '__fish_docker_no_subcommand' -a run -d 'Create and run a new container from an image'
457+
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)'
456458
complete -c docker -A -f -n '__fish_seen_subcommand_from run' -s a -l attach -d 'Attach to STDIN, STDOUT or STDERR.'
457459
complete -c docker -A -f -n '__fish_seen_subcommand_from run' -l add-host -d 'Add a custom host-to-IP mapping (host:ip)'
458460
complete -c docker -A -f -n '__fish_seen_subcommand_from run' -s c -l cpu-shares -d 'CPU shares (relative weight)'

contrib/completion/zsh/_docker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ __docker_container_subcommand() {
602602
opts_create_run=(
603603
"($help -a --attach)"{-a=,--attach=}"[Attach to stdin, stdout or stderr]:device:(STDIN STDOUT STDERR)"
604604
"($help)*--add-host=[Add a custom host-to-IP mapping]:host\:ip mapping: "
605+
"($help)*--annotation=[Add an annotation to the container (passed through to the OCI runtime)]:annotations: "
605606
"($help)*--blkio-weight-device=[Block IO (relative device weight)]:device:Block IO weight: "
606607
"($help)*--cap-add=[Add Linux capabilities]:capability: "
607608
"($help)*--cap-drop=[Drop Linux capabilities]:capability: "

docs/reference/commandline/container_create.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Create a new container
1212
| Name | Type | Default | Description |
1313
|:--------------------------|:--------------|:----------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
1414
| `--add-host` | `list` | | Add a custom host-to-IP mapping (host:ip) |
15+
| `--annotation` | `map` | `map[]` | Add an annotation to the container (passed through to the OCI runtime) |
1516
| `-a`, `--attach` | `list` | | Attach to STDIN, STDOUT or STDERR |
1617
| `--blkio-weight` | `uint16` | `0` | Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) |
1718
| `--blkio-weight-device` | `list` | | Block IO weight (relative device weight) |

docs/reference/commandline/container_run.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Create and run a new container from an image
1212
| Name | Type | Default | Description |
1313
|:--------------------------|:--------------|:----------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
1414
| `--add-host` | `list` | | Add a custom host-to-IP mapping (host:ip) |
15+
| `--annotation` | `map` | `map[]` | Add an annotation to the container (passed through to the OCI runtime) |
1516
| `-a`, `--attach` | `list` | | Attach to STDIN, STDOUT or STDERR |
1617
| `--blkio-weight` | `uint16` | `0` | Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) |
1718
| `--blkio-weight-device` | `list` | | Block IO weight (relative device weight) |

docs/reference/commandline/create.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Create a new container
1212
| Name | Type | Default | Description |
1313
|:--------------------------|:--------------|:----------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
1414
| `--add-host` | `list` | | Add a custom host-to-IP mapping (host:ip) |
15+
| `--annotation` | `map` | `map[]` | Add an annotation to the container (passed through to the OCI runtime) |
1516
| `-a`, `--attach` | `list` | | Attach to STDIN, STDOUT or STDERR |
1617
| `--blkio-weight` | `uint16` | `0` | Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) |
1718
| `--blkio-weight-device` | `list` | | Block IO weight (relative device weight) |

docs/reference/commandline/run.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Create and run a new container from an image
1212
| Name | Type | Default | Description |
1313
|:----------------------------------------------|:--------------|:----------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
1414
| [`--add-host`](#add-host) | `list` | | Add a custom host-to-IP mapping (host:ip) |
15+
| `--annotation` | `map` | `map[]` | Add an annotation to the container (passed through to the OCI runtime) |
1516
| [`-a`](#attach), [`--attach`](#attach) | `list` | | Attach to STDIN, STDOUT or STDERR |
1617
| `--blkio-weight` | `uint16` | `0` | Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) |
1718
| `--blkio-weight-device` | `list` | | Block IO weight (relative device weight) |

man/docker-run.1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ docker-run - Create and run a new container from an image
77
**docker run**
88
[**-a**|**--attach**[=*[]*]]
99
[**--add-host**[=*[]*]]
10+
[**--annotation**[=*[]*]]
1011
[**--blkio-weight**[=*[BLKIO-WEIGHT]*]]
1112
[**--blkio-weight-device**[=*[]*]]
1213
[**--cpu-shares**[=*0*]]
@@ -125,6 +126,11 @@ each of stdin, stdout, and stderr.
125126
Add a line to /etc/hosts. The format is hostname:ip. The **--add-host**
126127
option can be set multiple times.
127128

129+
**--annotation**=[]
130+
Add an annotation to the container (passed through to the OCI runtime).
131+
132+
The annotations are provided to the OCI runtime.
133+
128134
**--blkio-weight**=*0*
129135
Block IO weight (relative weight) accepts a weight value between 10 and 1000.
130136

0 commit comments

Comments
 (0)