Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command-line completion? #8550

Closed
sbellem opened this issue Feb 22, 2021 · 57 comments
Closed

Command-line completion? #8550

sbellem opened this issue Feb 22, 2021 · 57 comments

Comments

@sbellem
Copy link
Contributor

sbellem commented Feb 22, 2021

I just wonder if there's a command completion like there is for the docker-compose command.

@ndeloof
Copy link
Contributor

ndeloof commented Feb 22, 2021

Should be easy to generate one, as Cobra command library includes support for generating completion scripts https://github.com/spf13/cobra/blob/master/shell_completions.md

@ulyssessouza ulyssessouza self-assigned this Feb 25, 2021
@mat007
Copy link
Member

mat007 commented May 26, 2021

Are we looking to reach parity with docker-compose or is this not a requirement?

@ulyssessouza ulyssessouza removed their assignment May 26, 2021
@ndeloof
Copy link
Contributor

ndeloof commented May 26, 2021

@mat007 we don't have any shell completion support for docker compose yet, so we can start with a minimal one, then would be nice we can rely on cobra's BashCompletionFunction or ValidArgsFunction. This would require the backend to be involved providing valid values, so maybe not as simple as it sounds with the current "backend-neutral" architecture.

@glenjamin
Copy link

In the mean time, does anyone know if there's a way to configure docker compose completion to fall back to the old docker-compose completion?

@Mahoney
Copy link

Mahoney commented Nov 16, 2021

In the mean time, does anyone know if there's a way to configure docker compose completion to fall back to the old docker-compose completion?

This might suggest a way to do that:
https://stackoverflow.com/questions/55447023/how-do-i-defer-shell-completion-to-another-command-in-bash-and-zsh

@SuperSandro2000
Copy link

There was some completion but it was removed in c28aec2

@ndeloof
Copy link
Contributor

ndeloof commented Nov 27, 2021

AFAICT offering command line completion for docker compose (space) isn't trivial due to the CLI plugin architecture, but we could automatically produce completion for docker-compose (when used standalone) using Cobra command support for this feature. Wdyt @ulyssessouza @mat007 ?

@mat007
Copy link
Member

mat007 commented Nov 27, 2021

I was pretty sure this was already in place, but apparently it’s either not fully implemented or not working.
We’ll have a look: internally tracked as https://docker.atlassian.net/browse/IL-768

@ndeloof
Copy link
Contributor

ndeloof commented Nov 27, 2021

IIRC @ulyssessouza experimented with this, but he was blocked by docker CLI plugin architecture (we can't extend the docker CLI completion script)
But for direct use of docker-compose binary, we could adopt https://gist.github.com/jsteenb2/0973c838b00940ee8ccdf958a79756af and also add support for completion functions to auto-complete flags / args values.

@melroy89
Copy link

This is very strange indeed. What happened with the bash auto completion. Otherwise I would have created a PR for upgrading docker-compose to v2.2.2 on https://github.com/docker/docker.github.io/

@ndeloof
Copy link
Contributor

ndeloof commented Dec 14, 2021

Please note the existing docker-compose bash completion still works with compose v2 when used as a standalone binary. But it would beed some update to add support for additional commands and flags

@stefanocudini
Copy link

This problem has been solved? in some version?

@stefanocudini
Copy link

Should be easy to generate one, as Cobra command library includes support for generating completion scripts https://github.com/spf13/cobra/blob/master/shell_completions.md
@ndeloof were you able to generate completion scripts? for compose v2?

@PHLAK
Copy link

PHLAK commented Feb 2, 2022

This problem has been solved? in some version?

No, this doesn't seem to be solved yet.

rsteube added a commit to carapace-sh/carapace-bin that referenced this issue Feb 8, 2022
seems docker-compose is integrated as compose subcommand in docker

see docker/compose#8550
@rsteube
Copy link

rsteube commented Feb 8, 2022

Ok i think i missed something 🤔 : Is docker compose invoking docker-compose or is it compiled into the docker binary?

@SuperSandro2000
Copy link

docker compose aka docker-compose v2 is a plugin for the docker command written in go and is not using any python stuff from docker-compose v1.

@rsteube
Copy link

rsteube commented Feb 9, 2022

@SuperSandro2000 thanks. I got the shift from python to go, but I didn't catch that it is integrated as plugin.

@wbolster
Copy link

wbolster commented Feb 9, 2022

for completeness: under the hood, docker's cli-plugins handling does invoke a docker-compose executable (which happens to be written in golang these days indeed), which typically lives in /usr/lib/docker/cli-plugins/docker-compose and should not be called directly. (this is similar to how git handles subcommand ‘plugins’)

@rsteube
Copy link

rsteube commented Feb 9, 2022

docker-compose executable (which happens to be written in golang these days indeed), which typically lives in /usr/lib/docker/cli-plugins/docker-compose and should not be called directly. (this is similar to how git handles subcommand ‘plugins’)

Yeah, I was worried about that as I've been invoking docker-compose (which still exists in my PATH) for the completions - should be docker compose then.

Think i got it working though:
asciicast

@rsteube
Copy link

rsteube commented Feb 15, 2022

IIRC @ulyssessouza experimented with this, but he was blocked by docker CLI plugin architecture (we can't extend the docker CLI completion script)

@ndeloof i think this should be possible if you can update the docker cli to a newer cobra version.
It changed in recent versions from full completion script generation to invoking the binary.

In docker cli you just add a subcommand for each (installed) plugin with flag parsing disabled like here.
Then in the ValidArgsFunction invoke the __complete subcommand of the plugin with args and toComplete as arguments similar to this (ignore the directive handling as it is not needed in this case).

The __complete command outputs sth. like this:

value
value\twithDescription
:4
# empty line

So just return lines[:len(lines)-2] and :4 parsed as integer (ShellCompDirecive).

@ndeloof
Copy link
Contributor

ndeloof commented Feb 17, 2022

Started experimenting with cobra 1.3.0 to adopt cobra (dynamic) completion v2 : docker/cli#3429

@rsteube
Copy link

rsteube commented Feb 17, 2022

Nice. If it's any help for anyone in the meantime: got my completer at carapace-bin.
Just source the script from carapace docker and it should work with the compose and buildx subcommands.

@RafalSkolasinski
Copy link

Seems fish completions did not get updated to include compose command https://github.com/docker/cli/tree/master/contrib/completion

@PhilippeAccorsi
Copy link

PhilippeAccorsi commented Jul 22, 2022

I am also un-happy to lost autocompletion with this new version :(

I not really understand why change docker-compose to docker compose if it is more difficult to use the command line.

With old approch I use some file like this docker-compose-xxxxx.yml and in each file I have some services (preprod, prod, etc.), so to start/stop service it is very easy to just write

docker-c + "tab key"
docker-compose -f docker-c + "tab key"
docker-compose -f docker-compose-xx + "tab key"
docker-compose -f docker-compose-xxxxx.yml up -d 

Now i need to write all, all the time (or use the history, ctrl+r, etc.).

I understand the reason to not use python version anymore but for user, it is not better/simple to use compose.

(Docker Compose version v2.6.0)

@PHLAK
Copy link

PHLAK commented Aug 1, 2022

completion for docker compose doesn't go any further than completing compose, completion after that seems to be faulty

I'm experiencing the same.

$ pacman --query docker-compose
docker-compose 2.9.0-1

$ docker compose <tab> <tab>
Display all 145 possibilities? (y or n)
a                    container.           exec                 more                 projects             Start
alternate            containers           Execute              name                 ps                   stop
an                   containers,          -f,                  networks             public               Stop
and                  containers.          --file               (never|always|auto)  pull                 stopped
--ansi               control              file                 of                   Pull                 string
ANSI                 Control              file)                on                   push                 stringArray
auto)                convert              file.                one-off              Push                 the
backward             Converts             files                Options:             real                 the,
between              Copy                 files/folders        [OPTIONS]            rebuild              time
binding.             cp                   filesystem           or                   Receive              to
build                create               first                output               remove               top
Build                Create               for                  -p,                  Removes              unpause
by                   created              Force                path                 restart              Unpause
canonical            Creates              format               pause                Restart              up
characters           (default             from                 Pause                rm                   Usage:
command              (default:            --help               platforms            run                  used

@otbutz
Copy link

otbutz commented Aug 2, 2022

@ndeloof what's the state of #9462 ?

@ndeloof
Copy link
Contributor

ndeloof commented Aug 2, 2022

#9462 was designed to demonstrate ability to provide command line completion from a docker CLI plugin, and get docker/cli#3429 approved.

@tbroyer
Copy link

tbroyer commented Sep 10, 2022

Is this supposed to work now? With Docker 20.10.18 and Docker Compose 20.10.2, on ArchLinux, I still get the same Display all 145 possibilities? (y or n) as above.

Docker 20.10.18 release notes: https://docs.docker.com/engine/release-notes/#201018

Add Bash completion for Docker Compose docker/cli#3752.

@waja
Copy link

waja commented Sep 13, 2022

Looks like it also doesn't work on Debian bullseye.

# dpkg -l | grep -E "(docker-ce|docker-compose-plugin)"
ii  docker-ce                         5:20.10.18~3-0~debian-bullseye amd64        Docker: the open-source application container engine
ii  docker-ce-cli                     5:20.10.18~3-0~debian-bullseye amd64        Docker CLI: the open-source application container engine
ii  docker-compose-plugin             2.10.2~debian-bullseye         amd64        Docker Compose (V2) plugin for the Docker CLI.

@cornfeedhobo
Copy link

$ ./docker-compose completion --help
unknown docker command: "compose completion"

$ ./docker-compose version
Docker Compose version v2.11.0

but the completion sub command exists...

@DANic-git
Copy link

doesn't work on Debian 10

dpkg -l | grep -E "(docker-ce|docker-compose-plugin)"
ii  docker-ce                                       5:20.10.18~3-0~debian-buster  amd64        Docker: the open-source application container engine
ii  docker-ce-cli                                   5:20.10.18~3-0~debian-buster  amd64        Docker CLI: the open-source application container engine
ii  docker-compose-plugin                           2.10.2~debian-buster          amd64        Docker Compose (V2) plugin for the Docker CLI.
docker compose TAB
Display all 145 possibilities? (y or n)

@diegoquintanav
Copy link

It's not the same as having completions for docker compose, but a solution is to use the docker-compose command completion with

sudo curl -L https://raw.githubusercontent.com/docker/compose/1.21.2/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

And then by creating your own version of docker-compose that forwards calls to docker compose. According to https://stackoverflow.com/a/72187587/5819113.

echo 'docker compose "$@"' | sudo tee /bin/docker-compose
sudo chmod +x /bin/docker-compose 
docker-compose version

@ddolcimascolo
Copy link

I'm in the same boat. Should you need any info from my setup just ask.

Cheers,
David

@cornfeedhobo
Copy link

My point is that the command exists, so ... why is it not being included in releases?

@sungvzer
Copy link

Is there an alternative for Zsh?

Additional info:

> docker -v      
Docker version 20.10.22, build 3a2c30b
> docker compose version                    
Docker Compose version v2.14.1
> cat /etc/issue
Ubuntu 22.04.1 LTS

@Saucistophe
Copy link

@sungvzer You may look for a specific plugin

@allenyllee
Copy link

allenyllee commented Aug 1, 2023

I upgrade to version 2.20.2, it still doesn't work, any progress?

ii  docker-ce                             5:20.10.12~3-0~ubuntu-focal              amd64        Docker: the open-source application container engine
ii  docker-ce-cli                         5:20.10.12~3-0~ubuntu-focal              amd64        Docker CLI: the open-source application container engine
ii  docker-ce-rootless-extras             5:20.10.12~3-0~ubuntu-focal              amd64        Rootless support for Docker.
ii  docker-compose-plugin                 2.20.2-1~ubuntu.20.04~focal              amd64        Docker Compose (V2) plugin for the Docker CLI.

@layoaster
Copy link

@allenyllee you might want check this docker/docs#16397 (comment)

@willzhang
Copy link

更新 Docker CLI 完成( )在等待docker/cli#3429发布(v20.10.17?)curl -L https://raw.githubusercontent.com/docker/cli/master/contrib/completion/bash/docker -o /usr/share/bash-completion/completions/docker时引起了注意。

It works for me in ubuntu 22.04 bash

root@ubuntu:~# docker compose 
build    cp       down     exec     kill     ls       port     pull     restart  run      stop     unpause  version  
config   create   events   images   logs     pause    ps       push     rm       start    top      up       

@gkze
Copy link

gkze commented Jan 16, 2024

Is there a way to use Bash completion with Zsh? I have bashcompinit loaded but am getting errors. Alternatively, since the CLI uses Cobra, can the completion be automatically generated for the corresponding shell?

@ndeloof
Copy link
Contributor

ndeloof commented Jan 16, 2024

completion is managed by the docker CLI, You can use docker completion zsh to generate the zsh completion script.

@gkze
Copy link

gkze commented Jan 16, 2024

Thanks, was using Zsh completion from contrib. Is it even needed anymore then?

@ndeloof
Copy link
Contributor

ndeloof commented Jan 16, 2024

I'm not sure about the current status for the "new" extensible completion.
@laurazard is docker/cli contrib still relevant or do we now recommend using cobra-generated completion script?

@robcxyz
Copy link

robcxyz commented Mar 12, 2024

Am I right in seeing the docker compose -f <file name in path that should complete> ... doesn't work? Seems like a pretty important completion if it was omitted and a regression from the prior version when file paths are a clear candidate for a completion.

Found docker/docs#16397 which added completions for mac but the zsh instructions don't seem applicable to linux.

@gkze
Copy link

gkze commented Mar 12, 2024

The contents of docker completion zsh are working fine for me for -f.

@gkze
Copy link

gkze commented Mar 12, 2024

Docs are a little outdated though - they point @ the contrib completion scripts which don't work for compose as a subcommand

@bonyiii
Copy link

bonyiii commented Apr 8, 2024

This worked for me with bash: sudo docker completion bash > /etc/bash_completion.d/docker.sh

@ConnorsApps
Copy link

Adding to .zshrc or .bashrc

zsh

~/.zshrc

source <(docker completion zsh)

bash

~/.bashrc

source <(docker completion bash)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet