docker / cli

The Docker CLI
Apache License 2.0
4.88k stars 1.92k forks source link

Enhancement: docker exec -container_index ... #2606

Open Jongy opened 4 years ago

Jongy commented 4 years ago

Often times I start a new container in one terminal, then docker exec something in another terminal. I use zsh and I have the completions installed, so docker exec <TAB> lets me to select a container quickly. However I think that accessing a container by index (i.e like cd -3 or git show HEAD~7) can also be useful and more convenient. docker exec -4 /bin/bash will refer to the fourth container in docker ps, and so on.

I thought to add it as part of the zsh Docker completion script, but perhaps it should be added in the CLI itself?

I couldn't find any existing issue requesting such a feature, hence opening a new one.

maximillianfx commented 4 years ago

Hey @Jongy, how are you? Docker cli provide the docker exec container_name or docker exec container_hash to achieve this point. Why this could be a significant improvement?

Jongy commented 4 years ago

Hey @maximillianfx , the idea I had is to add another shortcut for naming containers in the Docker CLI (I mentioned docker exec specifically, but this applies to all subcommands).

Autocompletion for container names / hashes is useful, but for faster typing, when I remember which containers I ran lately, I find the cd -X notation more convenient - that is, faster to type.

michelhe commented 3 years ago

+1, I hate using the zsh completions as they are currently.. too much time wasted on docker exec my_cont<TAB> and then going back with the cursor before the container name to add the -it (the zsh completions don't work if you add the CLI switches before the container name)

cpuguy83 commented 3 years ago

-1 will probably be problematic, but possibly a literally just doing something like ~3 could work. Bash doesn't seem to interpret ~ in that case, at least.

Fwiw I keep a shell function, dplq() { docker ps -lq }, to get my last container.