‘ docker ps’输出格式: 只列出正在运行的容器的名称

第一行输出 docker ps --format "table {{.Names}}":

root@docker-2gb-blr1-01:~# docker ps --format "table {{.Names}}"
NAMES
enr
osticket
osticket_db
...

docker inspect --format '{{.Name}}' $(docker ps -q)

在容器名称的开头打印 /:

root@docker-2gb-blr1-01:~# docker inspect --format '{{.Name}}' $(docker ps -q)"
/enr
/osticket
/osticket_db

我只想列出正在运行的容器的名称,在开头没有标题或斜杠。请分享如何做到这一点的选项。

58626 次浏览

Try removing the table part from your --format argument, such as:

docker ps --format '\{\{.Names}}'

It should give you a simple list of container names with no table heading

Here is how we query the other columns with docker ps.

Names:

docker ps --format '\{\{.Names}}'

ID:

docker ps --format '\{\{.ID}}'

Image:

docker ps --format '\{\{.Image}}'

Command:

docker ps --format '\{\{.Command}}'

Created:

docker ps --format '\{\{.RunningFor}}'

Status:

docker ps --format '\{\{.Status}}'

Ports:

docker ps --format '\{\{.Ports}}'

More information can be found here.

Just a combination command, it is prettified with table.

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


IMAGE               PORTS                NAMES
nginx               0.0.0.0:80->80/tcp   nginx

In addition you can add it to .docker/config.json file that will allow you to customize the output of your docker ps command.

add to ~/.docker/config.json:

{
"psFormat": "table \{\{.ID}}\\t\{\{.Image}}\\t\{\{.Status}}\\t\{\{.Names}}"
}

docker ps --format "\nDocker Details \nID:\{\{.ID}} \nNames: \{\{.Names}} \nSize: \{\{.Size}} "

looks like

enter image description here

Ref : https://docs.docker.com/engine/reference/commandline/ps/#formatting