How to list containers in Docker

2019-01-20 20:48发布

There's a command to list images, docker images, but there doesn't seem to be a corresponding docker containers.

Other than becoming root and looking into /var/lib/docker there doesn't seem a way to do that. Am I missing something? Is that something one isn't supposed to do?

标签: docker
12条回答
smile是对你的礼貌
2楼-- · 2019-01-20 21:23

There are many ways to list all containers.

You can find using 3 Aliasesls, ps, list like this.

sudo docker container ls 
sudo docker container ps
sudo docker container list
sudo docker ps
sudo docker ps -a

You can also use give option[option].

Options -:

  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        Show last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display numeric IDs
  -s, --size            Display total file sizes

You can use an option like this:

sudo docker ps //Showing only running containers
sudo docker ps -a //All container (running + stopped)
sudo docker pa -l // latest
sudo docker ps -n <int valuse 1,2,3 etc>// latest number of created containers
sudo docker ps -s // Display container with size
sudo docker ps -q // Only display numeric IDs for containers
docker docker ps -a | tail -n 1 //oldest container
查看更多
我只想做你的唯一
3楼-- · 2019-01-20 21:24

To list all running and stopped containers

docker ps -a

To list all running containers (just stating the obvious and also example use of -f filtering option)

docker ps -a -f status=running

To list all running and stopped containers, showing only their container id

docker ps -aq

To remove all containers that are NOT running

docker rm `docker ps -aq -f status=exited`
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-20 21:24

I got the error message Cannot connect to the Docker daemon. I forgot I am running the daemon as root and needed sudo:

$ sudo docker ps
查看更多
虎瘦雄心在
5楼-- · 2019-01-20 21:25

The Docker command set is simple and holds together well:

docker stack ls
docker service ls
docker image ls
docker container ls

Teaching the aliases first is confusing. Once you understand what's going on, they can save some keystrokes:

docker images -> docker image ls
docker ps -> docker container ls
docker rmi -> docker image rm
docker rm -> docker container rm

There are several aliases in Docker. For instance:

docker rmi
docker image rm
docker image rmi
docker image remove

are all the same command (see for your self using docker help image rm).

查看更多
一夜七次
6楼-- · 2019-01-20 21:27
docker ps [OPTIONS]

Following command will show only running containers by default.

docker ps

To see all containers:

docker ps -a

For showing the latest created container:

docker ps -l
查看更多
▲ chillily
7楼-- · 2019-01-20 21:31

Command to get all containers ::

docker ps -a

Command to get running container::

docker ps
查看更多
登录 后发表回答