cli icon indicating copy to clipboard operation
cli copied to clipboard

Docker autocompletion for containers not working on MacOS

Open Theiaz opened this issue 1 year ago • 17 comments

Description

I'm migrating from Ubuntu to MacOS with my MacBook Pro (M2). I'm using ohmyzsh with the docker and docker-compose plugin. Docker has been installed with Docker Desktop setup.

The docker plugin works fine except the autocompletion for the container names.

  • If I type docker and ⮕Tab it shows me a list of commands
  • If I type docker start and ⮕Tab it just shows me the files of my current directory, but not the list of available containers.

I'm expecting the autosuggestion to work here and show me a list of all containers which could be started. This worked fine within Ubuntus bash.

What I did so far:

  • deactivate ohmyzsh (no completions at all)
  • ask google (obviously)
  • try the help sections for common problems on the ohmyzsh FAQ
  • ask apple stackexchange
  • ask ohmyzsh community who referred me to ask here

Screenshots and recordings

.zshrc

# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# history
SAVEHIST=10000

# ohmyzsh
ZSH_THEME="lukerandall"
zstyle ':omz:update' mode reminder  # just remind me to update when it's time
# docker autocompletion
#zstyle ':completion:*:*:docker:*' option-stacking yes
#zstyle ':completion:*:*:docker-*:*' option-stacking yes
plugins=(brew docker docker-compose git kubectl kubectx)
source $ZSH/oh-my-zsh.sh

# nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

autoload -U compinit && compinit

# kubectl
alias k="kubectl"
# krew
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"


#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="$HOME/.sdkman"
[[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && source "$HOME/.sdkman/bin/sdkman-init.sh"

Screenshots: Screenshot 2023-07-05 at 16 41 19 Screenshot 2023-07-05 at 16 41 38 Screenshot 2023-07-05 at 16 42 11

Reproduce

docker start+ ⮕Tab

Expected behavior

Show list of containers which are ready to start

docker version

Client:
 Cloud integration: v1.0.33
 Version:           24.0.2
 API version:       1.43
 Go version:        go1.20.4
 Git commit:        cb74dfc
 Built:             Thu May 25 21:51:16 2023
 OS/Arch:           darwin/arm64
 Context:           desktop-linux

Server: Docker Desktop 4.20.1 (110738)
 Engine:
  Version:          24.0.2
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.4
  Git commit:       659604f
  Built:            Thu May 25 21:50:59 2023
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.21
  GitCommit:        3dce8eb055cbb6872793272b4f20ed16117344f8
 runc:
  Version:          1.1.7
  GitCommit:        v1.1.7-0-g860f061
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker info

Client:
 Version:    24.0.2
 Context:    desktop-linux
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.10.5
    Path:     /Users/julian/.docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.18.1
    Path:     /Users/julian/.docker/cli-plugins/docker-compose
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.1.0
    Path:     /Users/julian/.docker/cli-plugins/docker-dev
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.19
    Path:     /Users/julian/.docker/cli-plugins/docker-extension
  init: Creates Docker-related starter files for your project (Docker Inc.)
    Version:  v0.1.0-beta.4
    Path:     /Users/julian/.docker/cli-plugins/docker-init
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)
    Version:  0.6.0
    Path:     /Users/julian/.docker/cli-plugins/docker-sbom
  scan: Docker Scan (Docker Inc.)
    Version:  v0.26.0
    Path:     /Users/julian/.docker/cli-plugins/docker-scan
  scout: Command line tool for Docker Scout (Docker Inc.)
    Version:  v0.12.0
    Path:     /Users/julian/.docker/cli-plugins/docker-scout

Server:
 Containers: 3
  Running: 2
  Paused: 0
  Stopped: 1
 Images: 5
 Server Version: 24.0.2
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 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: 3dce8eb055cbb6872793272b4f20ed16117344f8
 runc version: v1.1.7-0-g860f061
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 5.15.49-linuxkit-pr
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 6
 Total Memory: 7.667GiB
 Name: docker-desktop
 ID: 235862c7-714f-4c03-a56e-fed6a8f78116
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  127.0.0.0/8
 Live Restore Enabled: false

Additional Info

macOS 13.14.1 Zsh version 5.9

Theiaz avatar Jul 05 '23 15:07 Theiaz

Hiya @Theiaz, thanks for the report. Indeed, the current zsh completion script is a bit broken for container name completion – I started looking into fixing this some time ago, but haven't had the time yet. A quick alternative is to use the Cobra V2 completion script generated by docker completion zsh: we haven't switched over to using these by default, but in my experience (at least for zsh), it seems to work pretty well.

You can try this out by running

$ source <(docker completion zsh)

laurazard avatar Jul 17 '23 10:07 laurazard

This has no effect, no matter if I run the command in the console or add it to my .zshrc.

Theiaz avatar Jul 18 '23 05:07 Theiaz

Do you mean the script generated by docker completion zsh doesn't do anything at all? I'd recommend checking the output of docker completion zsh --help to see some more information on how to enable zsh completions (these can be different depending on environment/OS, so maybe you'll see something relevant – such as making sure autocompletions are enabled with echo "autoload -U compinit; compinit" >> ~/.zshrc).

laurazard avatar Jul 18 '23 11:07 laurazard

I have the same problem. If I use source <(docker completion zsh), and then enable debug logging with export BASH_COMP_DEBUG_FILE=~/docker-completion.log; touch $BASH_COMP_DEBUG_FILE, I get the following:

========= starting completion logic ==========
CURRENT: 3, words[*]: docker rm
Truncated words[*]: docker rm ,
lastParam: , lastChar:
Adding extra empty parameter
About to call: eval docker __completeNoDesc rm  ""
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

This with docker rm <TAB> and a number of stopped containers.

rlipscombe avatar Jul 31 '23 18:07 rlipscombe

Hello Guys,

Same here!

========= starting completion logic ==========
CURRENT: 3, words[*]: docker rm
Truncated words[*]: docker rm ,
lastParam: , lastChar:
Adding extra empty parameter
About to call: eval docker __completeNoDesc rm  ""
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

this was triggered by docker rm <TAB>

Further: docker images paramtere is fully missing:

========= starting completion logic ==========
CURRENT: 2, words[*]: docker
Truncated words[*]: docker ,
lastParam: , lastChar:
Adding extra empty parameter
About to call: eval docker __completeNoDesc  ""
completion output: completion
compose
context
ecs
exec
help
inspect
kill
login
logout
logs
prune
ps
rm
run
secret
serve
start
stop
version
volume
:4
last line: :4
directive: 4
completions: completion
compose
context
ecs
exec
help
inspect
kill
login
logout
logs
prune
ps
rm
run
secret
serve
start
stop
version
volume
flagPrefix:
Adding completion: completion
Adding completion: compose
Adding completion: context
Adding completion: ecs
Adding completion: exec
Adding completion: help
Adding completion: inspect
Adding completion: kill
Adding completion: login
Adding completion: logout
Adding completion: logs
Adding completion: prune
Adding completion: ps
Adding completion: rm
Adding completion: run
Adding completion: secret
Adding completion: serve
Adding completion: start
Adding completion: stop
Adding completion: version
Adding completion: volume
Calling _describe
_describe found some completions

image

lagunary avatar Aug 01 '23 13:08 lagunary

@Theiaz did you installed docker desktop with download and install or with brew install --cask docker? Ive tried both way. In docker desktop installation (default) there is no docker 'image' argument. When you install docker with brew cask you will have another version and location of docker binary and it contains the image argumentum, but as you wrote, it doesn't work as same as docker rm is not working.

lagunary avatar Aug 07 '23 08:08 lagunary

@Theiaz did you installed docker desktop with download and install or with brew install --cask docker?

I did it the official recommended way with .dmg file.

Theiaz avatar Aug 07 '23 10:08 Theiaz

Hello there, if I want to contribute to this, how to contribute ?

rakeshkhopade12 avatar Aug 10 '23 16:08 rakeshkhopade12

I have the same issue - Docker desktop and Mac OS Ventura on ZSH with Oh My ZSH.

geekbleek avatar Aug 23 '23 22:08 geekbleek

Same for me - I really miss being able to autocomplete running containers

dacox avatar Aug 25 '23 22:08 dacox

Homebrew brew will download completion automatically when install a majority of application like docker. the completion file will be stored in $(brew --prefix)/share/zsh/site-functions/. but I still got some mojibake after source _docker completion file. Screen Shot 2023-09-08 at 10 50 21 Screen Shot 2023-09-08 at 10 55 11 Screen Shot 2023-09-08 at 10 58 45 Screen Shot 2023-09-08 at 11 00 36

ShIRannx avatar Sep 08 '23 03:09 ShIRannx

Same here, not only docker start, but also the same for docker run and other subcomands. I type docker then tab it shows an incomplete command list.

amchii avatar Sep 08 '23 10:09 amchii

Hi! There is any update for this issue?a

ahmetkotan avatar Oct 11 '23 23:10 ahmetkotan

Hi all, just find this issue on my macbook, I install docker through download .dmg docker desktop, and setting $PATH env to use docker command on zsh, like this:

# ~/.zshrc
export PATH="$PATH:$HOME/.docker/bin"
plugins=(
...
docker
docker-compose
...
)

and my docker command can use completions, but it’s likely older version (not information for every sub-command and if I type docker rm <tab> it will not check my system container ID)

like this:

image

and I using find command check all _docker file, I get result:

find ~ 2>/dev/null | grep _docker
image

and the first one is auto-generate if you setting at ~/.zshrc plugins=(docker), but it’s older version (I think this from docker completion zsh )

the correct one is/Users/$USER/.oh-my-zsh/plugins/completions/_docker , but at my macbook, if you check $FPATH envirable, there is no have /Users/$USER/.oh-my-zsh/plugins/docker/completions/ image

and there have /Users/$USER/.oh-my-zsh/plugins/docker , so I create Symbolic link through:

ln -s /Users/$USER/.oh-my-zsh/plugins/docker/completions/_docker /Users/$USER/.oh-my-zsh/plugins/docker/_docker
# restart zsh or source ~/.zshrc
source ~/.zshrc

NOTE: If you don’t have /Users/$USER/.oh-my-zsh/plugins/docker/completions/_docker you can get from here:https://raw.githubusercontent.com/docker/cli/master/contrib/completion/zsh/_docker

and now, docker command can get my tab like this:

image image

Not a very pretty solution, but works for me.

hope this help someone :)


This my environment reference:

  • OS: macOS sonama 14.0
# docker version  
Client:
 Cloud integration: v1.0.35+desktop.5
 Version:           24.0.6
 API version:       1.43
 Go version:        go1.20.7
 Git commit:        ed223bc
 Built:             Mon Sep  4 12:28:49 2023
 OS/Arch:           darwin/arm64
 Context:           desktop-linux

Server: Docker Desktop 4.25.0 (126437)
 Engine:
  Version:          24.0.6
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.7
  Git commit:       1a79695
  Built:            Mon Sep  4 12:31:36 2023
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.22
  GitCommit:        8165feabfdfe38c65b599c4993d227328c231fca
 runc:
  Version:          1.1.8
  GitCommit:        v1.1.8-0-g82f18fe
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
# omz version
master (38c01a2)

RayLin9981 avatar Oct 31 '23 10:10 RayLin9981

docker network is also missing (not only for me from what I see on the screenshots).

jkulak avatar Nov 03 '23 21:11 jkulak

At the moment, the introduction of the new docker completion script (docker completion zsh) isn't working very well. As I'm using oh-my-zsh, I would suggest using the legacy completion script as described here : https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/docker/README.md. It fixed the issue for me.

Further explanation here : https://github.com/ohmyzsh/ohmyzsh/issues/11789

marcantoinegodde avatar Nov 05 '23 12:11 marcantoinegodde

@RayLin9981 thank you for details troubleshooting. It helped me a lot.

khoujamoujahed avatar Feb 06 '24 15:02 khoujamoujahed