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

后端 未结 3 784
别那么骄傲
别那么骄傲 2020-11-27 20:52

The docker command has a ps sub-command that emits very long lines:

$ docker ps -a
CONTAINER ID        IMAGE                                


        
3条回答
  •  再見小時候
    2020-11-27 21:22

    Using Docker Config

    Since this is fundamentally a docker questions, not a bash question, you don't even need an alias. Docker CLI allows you to customize these commands in your own config file! From this great tip from Container 42:

    1. Create or find your docker config file (if you've ever used docker login it should already be created.

      ~/.docker/config.json
      
    2. Then add the default formatting for docker to use every time it runs the ps command as a top level property in the config:

      {
          "psFormat": "table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}",
      }
      
    3. Then just run docker ps like normal:

    PS Format

    Docker uses go templates and has a list of the valid placeholders:

    | Command      | Description                                       |
    | ------------ | ------------------------------------------------- |
    | .ID          | Container ID                                      |
    | .Image       | Image ID                                          |
    | .Command     | Quoted command                                    |
    | .CreatedAt   | Time when the container was created.              |
    | .RunningFor  | Elapsed time since the container was started.     |
    | .Ports       | Exposed ports.                                    |
    | .Status      | Container status.                                 |
    | .Size        | Container disk size.                              |
    | .Names       | Container names.                                  |
    | .Labels      | All labels assigned to the container.             |
    | .Label       | Value of a specific label for this container.     |
    | .Mounts      | Names of the volumes mounted in this container.   |
    | .Networks    | Names of the networks attached to this container. |
    

    Alternative Solutions / Threads

    • Github Issues
      • Default "docker ps" output is too wide
      • docker ps output is so long it's unreadable
    • Third Party Commands
      • ctop - Top-like interface for container metrics
      • docker-pretty-ps - beautiful, colored, long output log
      • dockerps - A better docker ps

提交回复
热议问题