Commit Graph

445 Commits

Author SHA1 Message Date
a07391c65d Merge pull request #5906 from thaJeztah/remove_client_warnings
fix duplicate warnings on docker run / docker create, and slight refactor
2025-03-10 16:03:38 +01:00
bc90bb6855 container create: combine client-side warning with daemon-side
Use a consistent approach for producing warnings, but add a TODO for moving
this warning to the daemon, which can make a better call if it will work
or not (depending on networking mode).

This warning was originally added in [moby@afa92a9], before integration with
libnetwork, and this warning may be incorrect in many scenarios.

While updating, also removing the custom regular expression used to
detect if the IP is a loopback address, and using go's netip package
instead.

[moby@afa92a9]: afa92a9af0

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-03-08 16:15:50 +01:00
58a35692d6 remove duplicate --oom-kill-disable warnings on docker run / docker create
This warning was originally added in [moby@3aa70c1], and moved to be printed
on both `run` and `create` in commit 7c514a31c9.

However, [moby@57f1305] (docker 19.03, API 1.40) moved such warnings to
the daemon side. The patch mentioned this issue:

> This patch will have one side-effect; docker cli's that also perform this check
> client-side will print the warning twice; this can be addressed by disabling
> the cli-side check for newer API versions, but will generate a bit of extra
> noise when using an older CLI.

The CLI does not take this into account currently, and still prints warnings
twice; even in cases where the option is not supported by the daemon, and
discarded:

On a host without OomKillDisable support:

    docker create --oom-kill-disable alpine
    WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.
    WARNING: Your kernel does not support OomKillDisable. OomKillDisable discarded.

On a host that supports it:

    docker create --oom-kill-disable alpine
    WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.
    WARNING: OOM killer is disabled for the container, but no memory limit is set, this can result in the system running out of resources.

This patch removes the client-side warning, leaving it to the daemon to
report if any warnings should produced (and the client to print them).

With this patch applied:

On a host without OomKillDisable support:

    docker create --oom-kill-disable alpine
    WARNING: Your kernel does not support OomKillDisable. OomKillDisable discarded.

On a host that supports it:

    docker create --oom-kill-disable alpine
    WARNING: OOM killer is disabled for the container, but no memory limit is set, this can result in the system running out of resources.

[moby@3aa70c1]: 3aa70c1948
[moby@57f1305]: 57f1305e74

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-03-08 15:39:21 +01:00
46caf5697c remove legacy build-tags
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-03-08 12:47:03 +01:00
e37d814ce9 cli/command/image: deprecate TagTrusted, move to cli/trust
This function was shared between "image" and "container" packages,
all of which needed the trust package, so move it there instead.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-03-05 21:36:53 +01:00
bfd49b1ec3 Merge pull request #5836 from thaJeztah/nakedret
golangci-lint: fix invalid nakedret config, disallow for any func length
2025-02-18 13:15:28 +01:00
a5020ea165 cli/command/container: don't use naked returns (nakedret)
cli/command/container/cp.go:206:3: naked return in func `resolveLocalPath` with 5 lines of code (nakedret)
            return
            ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-18 09:36:11 +01:00
e71380eb5b cli/command/container: fix "unused-receiver" linting
cli/command/container/client_test.go:78:7: unused-receiver: method receiver 'f' is not referenced in method's body, consider removing or renaming it as _ (revive)
    func (f *fakeClient) ContainerExecStart(context.Context, string, container.ExecStartOptions) error {
          ^
    cli/command/container/create_test.go:383:7: unused-receiver: method receiver 'f' is not referenced in method's body, consider removing or renaming it as _ (revive)
    func (f fakeNotFound) NotFound()     {}
          ^
    cli/command/container/create_test.go:384:7: unused-receiver: method receiver 'f' is not referenced in method's body, consider removing or renaming it as _ (revive)
    func (f fakeNotFound) Error() string { return "error fake not found" }
          ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-17 15:24:56 +01:00
3825d37923 cli/command: define some consts for repeated values
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-17 15:24:44 +01:00
e868f0f580 cli/command/container/opts_test: Fix entrypoint parsing logic
Right now the test passes even if you change the expected value.
It passes if the array has 1 element.

Signed-off-by: Lajos Papp <lalyos@yahoo.com>
2025-02-17 11:10:50 +00:00
0eda4c51d7 Merge pull request #5818 from mohammedfuta2000/5814-limit-completion
Restrict completion to one suggestion on run, history, push, tag
2025-02-13 13:54:08 +01:00
a656dfd409 Restrict completion for some commands with a limit paramter for ImageNames
Previously, multiple suggestions were provided when completing
commands like `run`, `history` and `push`. This change
limits completion to a single suggestion for the above and 2 suggestions for `tag`

Signed-off-by: Mohammed Aminu Futa <mohammedfuta2000@gmail.com>
2025-02-12 12:42:19 +00:00
c6a7f9a646 vendor: github.com/docker/docker b570831cc3a3 (master, v28.0.0-rc.2)
full diff: https://github.com/docker/docker/compare/v28.0.0-rc.1...b570831cc3a3fcfe4edc96af4c249199b019c7dd

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-11 18:06:55 +01:00
85a77af591 Don't print "context canceled" if user terminated
Without breaking API compatibility, this patch allows us to know whether
a returned `cli/StatusError` was caused by a context cancellation or
not, which we can use to provide a nicer UX and not print the Go
"context canceled" error message if this is the cause.

Signed-off-by: Laura Brehm <laurabrehm@hey.com>
2025-02-06 14:16:20 +01:00
dff0dc8afa Merge pull request #5790 from thaJeztah/leftover_cleanups
cli/command: some minor cleanups
2025-02-04 12:30:30 +00:00
150f27b68c cli/command/container: use errors.Join
Use stdlib multi-errors instead of creating our own

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-03 19:17:50 +01:00
e81d76ffe1 cli/command/container: minor cleanups
- use apiClient instead of client for the API client to
  prevent shadowing imports.
- use dockerCLI with Go's standard camelCase casing.
- suppress some errors to make my IDE and linters happier

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-03 19:08:08 +01:00
c462eaee11 cli/command/container: minor cleanups: use Println
- use Println to print newline instead of custom format
- suppress some errors to make my IDE and linters happier

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-03 12:18:10 +01:00
5a99ea9ad4 cli/command/container: remove uses of pkg/errors in tests
While there may be reasons to keep pkg/errors in production code,
we don't need them for these tests.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-01 15:59:57 +01:00
2c35778a63 Allow '--link' with '--network bridge'
The '--link' option should only be migrated to an endpoint
option if the network is user-defined ... there was already
an exception for network "default", but not for "bridge".

Signed-off-by: Rob Murray <rob.murray@docker.com>
2025-01-21 16:58:42 +00:00
91adb70d6b pkg/command: wrap jsonmessage.DisplayJSONMessagesStream with go context
Allows for the `jsonmessage.DisplayJSONMessagesStream` function
to correctly return when the context is cancelled with the appropriate
reason (`ctx.Error()`) instead of just a nil error.

Follow-up to 30a73ff19c

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
Co-authored-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-01-20 16:36:11 +01:00
8c0cb30515 Fix cp test to separate source and destination
Currently the cp will tar from the same directory it will untar into
simultaneously. There is a race between reading the file and truncating
the file for write, however, the race will not show up with a large
enough buffer on the tar side if buffered before the copy begins.

Also removes the unnecessary deferred removal, the removal is handled by
cleanup and respects the no cleanup env.

Signed-off-by: Derek McGowan <derek@mcg.dev>
2024-12-26 16:30:45 -08:00
a8f83d5d99 TestRunCopyFromContainerToFilesystem: use Tar without options
Just a minor cleanup; use archive.Tar as we're not using other
options here.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-12-23 13:23:39 +01:00
1eda498786 cli/command/container: use local copy of pkg/system.IsAbs
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-12-16 10:22:35 +01:00
e554bfef6c Merge pull request #5662 from laurazard/consistent-attach-check
run: correctly handle only STDIN attached
2024-12-09 18:01:45 +01:00
30c4637f03 run: don't hang if only attaching STDIN
If STDOUT or STDERR are attached and the container exits, the streams
will be closed by the daemon while the container is exiting, causing
the streamer to return an error
61b02e636d/cli/command/container/hijack.go (L53)
that gets sent
61b02e636d/cli/command/container/run.go (L278)
and received
61b02e636d/cli/command/container/run.go (L225)
on `errCh`.

However, if only STDIN is attached, it's not closed (since this is
attached to the user's TTY) when the container exits, so the streamer
doesn't exit and nothing gets sent on `errCh`, meaning the CLI execution
hangs receiving on `errCh` on L231.

Change the logic to receive on both `errCh` and `statusChan` – this way,
if the container exits, we get notified on `statusChan` (even if only
STDIN is attached), and can cancel the streamer and exit.

Signed-off-by: Laura Brehm <laurabrehm@hey.com>
2024-12-09 15:49:20 +00:00
d4db289eb5 run, create, connect: add support for gw-priority
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2024-12-03 15:13:08 +01:00
a3d9fc4941 run: cleanup – remove errCh nil check
Now, if running in "detached" mode, we early exit at L222.

Similarly, if `attachContainer` errors out, it returns an error that
gets handled on L190.

As such, `errCh` can never be nil on L231. Remove the nil check.

Signed-off-by: Laura Brehm <laurabrehm@hey.com>
2024-12-02 14:35:30 +00:00
446f36ce58 run: cleanup – move "detached" early exit earlier
Since everything else after the `apiClient.ContainerStart` block is
under an `if attach` conditional, we can move the "detached" early exit
up.

Signed-off-by: Laura Brehm <laurabrehm@hey.com>
2024-12-02 14:35:29 +00:00
8431298824 run: cleanup – use attached where applicable
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
2024-12-02 14:34:28 +00:00
aee9eebf34 run: return error code when only STDIN attached
During a `docker run`, the CLI has some different behavior/output
depending on whether the run is "detached" or not.

In some cases, the CLI is checking whether either `stdin`, `stdout` or
`stderr` are attached, but in other cases we're only checking `stdout`
and `stderr`, which leads to some inconsistencies:

```
$ docker run -a stdout --rm --name test alpine top
[docker kill test]
exit status 137

$ docker run -a stderr --rm --name test alpine top
[docker kill test]
exit status 137

$ docker run -a stdin --rm --name test alpine top
56820d94a89b96889478241ae68920323332c6d4cf9b51ba9340cba01e9e0565
[docker kill test]
[no exit code]
```

Since we're not checking for whether `stdin` is attached when deciding
whether to early exit without receiving on `statusChan`, the `docker run
-a stdin` is falling into the "detached mode" logic, which simply prints
the container ID and doesn't print/return the exit code.

This patch makes the "attached" checks consistent.

Signed-off-by: Laura Brehm <laurabrehm@hey.com>
2024-12-02 14:31:26 +00:00
30a73ff19c fix: ctx should cancel image pull on run
This patch fixes the context cancellation
behaviour for the `runContainer` function,
specifically the `createContainer` function
introduced in this commit 991b1303da.

It delays stripping the `cancel` from the context
passed into the `runContainer` function so that
the `createContainer` function can be cancelled
gracefully by a SIGTERM/SIGINT.

This is especially true when the requested image
does not exist and `docker run` needs to `pull`
the image before creating the container.

Although this patch does gracefully cancel
the `runContainer` function it does not address
the root cause. Some functions in the call path
are not context aware, such as `pullImage`.

Future work would still be necessary to ensure
a consistent behaviour in the CLI.

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
2024-11-28 09:53:04 +01:00
cb2f95ceee Optimise docker stats to not require clearing the whole screen
Instead of clearing the whole screen and then writing the new stats,
we now write the new stats on top of the old text, and then clear
the remaining text.

This is a more efficient way to update the stats, as it avoids the
flickering that happens when the screen is cleared and rewritten.

Signed-off-by: Giedrius Jonikas <giedriusj1@gmail.com>
2024-11-16 15:29:57 +00:00
d1d5353269 cli/command/container: fix missing go:build tag
make shell
    make -C ./internal/gocompat/

    GO111MODULE=on go test -v
    # github.com/docker/cli/cli/command/container
    ../../cli/command/container/completion.go:37:28: implicit function instantiation requires go1.18 or later (-lang was set to go1.16; check go.mod)
    ../../cli/command/container/completion.go:82:25: implicit function instantiation requires go1.18 or later (-lang was set to go1.16; check go.mod)
    ../../cli/command/container/completion.go:92:27: implicit function instantiation requires go1.18 or later (-lang was set to go1.16; check go.mod)
    FAIL	gocompat [build failed]

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-15 11:06:20 +01:00
4adbb18f1c Merge pull request #5580 from albers/container-completions
Improve Cobra completions for `run` and `create`
2024-11-14 16:58:54 +01:00
7c80e4f938 update go:build tags to use go1.22
commit 4a7b04d412 configured golangci-lint
to use go1.23 semantics, which enabled the copyloopvar linter.

go1.22 now creates a copy of variables when assigned in a loop; make sure we
don't have files that may downgrade semantics to go1.21 in case that also means
disabling that feature; https://go.dev/ref/spec#Go_1.22

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-12 12:38:18 +01:00
06260e68f3 Handle null completions with a default callback
Credits to thaJeztah

Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:55:59 +00:00
4525fe37b4 Add completion for --volume-driver
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:55:59 +00:00
db0ed1e216 Add completion for --cgroupns
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:55:59 +00:00
2915749279 Add completion for --uts
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:55:59 +00:00
3a2503fa43 Add completion for --log-driver and --log-opt
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:55:59 +00:00
9a9ae231a9 Add completion for --security-opt
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:55:59 +00:00
5f7c43e5e6 Add completion for --detach-keys
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:55:59 +00:00
3292afe6e6 Add completion for --userns
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:55:59 +00:00
5d709a8d9f Add completion for --ulimit
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:55:59 +00:00
2d89339b34 Add completion for --storage-opt
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:55:59 +00:00
ac7bde6f64 Add completion for --pid
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:55:59 +00:00
e513454244 Add completion for --link
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:35:34 +00:00
c555327f0b Add completion for --ipc
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:35:34 +00:00
b598ec8cdb Add completion for --attach
Signed-off-by: Harald Albers <github@albersweb.de>
2024-11-08 15:35:34 +00:00