cli icon indicating copy to clipboard operation
cli copied to clipboard

Shell completion for images is not working

Open mathieu-lemay opened this issue 9 months ago • 3 comments

Description

The shell completion generated by docker completion doesn't work for images. If I enable completion debugging, by setting the BASH_COMP_DEBUG_FILE environment variable, I get the following information:

========= starting completion logic ==========
CURRENT: 3, words[*]: docker rmi
Truncated words[*]: docker rmi ,
lastParam: , lastChar:
Adding extra empty parameter
About to call: eval docker __completeNoDesc rmi  ""
completion output: :0
last line: :0
directive: 0
completions:
flagPrefix:
Calling _describe
_describe did not find completions.
Checking if we should do file completion.
Activating file completion

We can see that the command docker __completeNoDesc rmi "" is being called, and that command always returns the following:

$ docker __completeNoDesc rmi ""
:0
Completion ended with directive: ShellCompDirectiveDefault

By comparison, docker __completeNoDesc run "" does list the available images:

$ docker __completeNoDesc run ""
alpine:latest
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

This issue can be worked around by using the completion files found in the contrib folder of this repository. Doing this however breaks the auto completion for docker compose, which is why the official Arch Linux package has stopped using it.

Reproduce

  1. Have at least one image available
  2. Ensure you are using the completion generated from docker completion: source <(docker completion <your shell here>)
  3. docker rmi <TAB>

Expected behavior

The shell completion should suggest a list of available images. Instead, it shows the files in the current directory.

docker version

Client:
 Version:           24.0.5
 API version:       1.43
 Go version:        go1.20.6
 Git commit:        ced0996600
 Built:             Wed Jul 26 21:44:58 2023
 OS/Arch:           linux/amd64
 Context:           default

Server:
 Engine:
  Version:          24.0.5
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.6
  Git commit:       a61e2b4c9c
  Built:            Wed Jul 26 21:44:58 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.7.6
  GitCommit:        091922f03c2762540fd057fba91260237ff86acb.m
 runc:
  Version:          1.1.9
  GitCommit:        
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker info

Client:
 Version:    24.0.5
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  0.11.2
    Path:     /usr/lib/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  2.20.3
    Path:     /usr/lib/docker/cli-plugins/docker-compose

Server:
 Containers: 1
  Running: 1
  Paused: 0
  Stopped: 0
 Images: 27
 Server Version: 24.0.5
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: true
  Native Overlay Diff: false
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 091922f03c2762540fd057fba91260237ff86acb.m
 runc version: 
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.5.5-arch1-1
 Operating System: Arch Linux
 OSType: linux
 Architecture: x86_64
 CPUs: 12
 Total Memory: 31.27GiB
 Name: ragnarok
 ID: O7FG:KKYF:CPWV:KSVB:NL4B:AUKS:4I6N:EEKZ:MXAV:R2ZZ:YLB6:EBRY
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Username: acidrain
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Additional Info

No response

mathieu-lemay avatar Sep 30 '23 16:09 mathieu-lemay

Hi @mathieu-lemay, Here's a quick fix for you, I think you are able to rebuild the docker package from arch yourself.

--- a/cli/command/image/remove.go
+++ b/cli/command/image/remove.go
@@ -7,6 +7,7 @@ import (
 
 	"github.com/docker/cli/cli"
 	"github.com/docker/cli/cli/command"
+	"github.com/docker/cli/cli/command/completion"
 	"github.com/docker/docker/api/types"
 	apiclient "github.com/docker/docker/client"
 	"github.com/pkg/errors"
@@ -32,6 +33,7 @@ func NewRemoveCommand(dockerCli command.Cli) *cobra.Command {
 		Annotations: map[string]string{
 			"aliases": "docker image rm, docker image remove, docker rmi",
 		},
+		ValidArgsFunction: completion.ImageNames(dockerCli),
 	}
 
 	flags := cmd.Flags()

vitoyucepi avatar Oct 04 '23 06:10 vitoyucepi

@vitoyucepi Thank you! It works perfectly fine. I've applied the fix in cli/command/image/inspect.go also.

mathieu-lemay avatar Oct 04 '23 12:10 mathieu-lemay

latest docker rootless by script (curl -fsSL https://get.docker.com/rootless | sh) image completion not work 's

     docker version
     Client:
     Version:           25.0.0
     API version:       1.44
     Go version:        go1.21.6
     Git commit:        e758fe5
     Built:             Thu Jan 18 17:09:01 2024
     OS/Arch:           linux/amd64

it seems to be a behavior of the engine in rootless mode, because using the alpine:edge binary (Git commit: afdd53b4e341be38d2056a42113b938559bb1d94), consuming the socket (docker run -v socket:socket, etc) also does not support completion for images.

vmath3us avatar Jan 20 '24 11:01 vmath3us