Can I use a subcommand? (shortening the output of `docker ps`)

The command dockerhas a subcommand psthat emits very long lines:

$ docker ps -a
CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS                      PORTS                                                                                                                                                                                                                                                                                                        NAMES
6e8ec8a16da4        waisbrot/wait:latest              "/wait"                  4 minutes ago       Exited (0) 4 minutes ago                                                                                                                                                                                                                                                                                                                 wait-for-janus-test
9dbf0739561f        whoop/downsampler:master          "./run.bash"             4 minutes ago       Up 4 minutes                0.0.0.0:32855->4369/tcp, 0.0.0.0:32854->9100/tcp, 0.0.0.0:32853->9101/tcp, 0.0.0.0:32852->9102/tcp, 0.0.0.0:32851->9103/tcp, 0.0.0.0:32850->9104/tcp, 0.0.0.0:32849->9105/tcp, 0.0.0.0:32848->9106/tcp, 0.0.0.0:32847->9107/tcp, 0.0.0.0:32846->9108/tcp, 0.0.0.0:32845->9109/tcp, 0.0.0.0:32844->9110/tcp   metrics-downsampler-test
6cf56623bb48        whoop/janus:master                "./start.bash"           4 minutes ago       Up 4 minutes                0.0.0.0:32843->80/tcp                                                                                                                                                                                                                                                                                        janus-test
882b50303d54        whoop/recalculator:master         "./run.bash"             4 minutes ago       Exited (1) 4 minutes ago                                                                                                                                                                                                                                                                                                                 internum-test

It may be suggested to display only certain columns:

docker ps --format "table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}"

I would like to say docker psand add an argument --format "table..."for me. Is there a good way to do this?

I know what I can say

alias dp='docker ps --format ...'

but I would rather keep the subcommand.

I use zsh as my shell.

+4
source share
1 answer

docker , . ( zsh, POSIX - , zsh ).

docker() {
  case $1 in
    ps)
      shift
      command docker ps --format 'table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}' "$@"
      ;;
    *)
      command docker "$@";;
  esac
}

( ps), ( , POSIX sh - local, , ash ):

docker() {
  local cmd=$1; shift
  if command -v "docker_$cmd" >/dev/null 2>/dev/null; then
    "docker_$cmd" "$@"
  else
    command docker "$cmd" "$@"
  fi
}

... , , ( script PATH docker_ps , ):

docker_ps() {
  command docker ps --format 'table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}' "$@"
}
+7

Source: https://habr.com/ru/post/1623959/


All Articles