【Self-study Docker】Docker ps command

Docker ps command

outline

insert image description here

Overview of the Docker ps command

The docker ps command can be used to list information about Docker containers .

Docker ps command syntax

haicoder(www.haicoder.net)# docker ps [OPTIONS]

Docker ps command parameters

options illustrate
No reference Shows running containers by default.
-a Show all containers, including non-running ones.
-f Filter the displayed content based on conditions.
–format Specifies the template file for the return value.
-l Show recently created containers.
-n List the most recently created n containers.
–no-trunc Output is not truncated.
-q In silent mode, only the container ID is displayed.
-s Displays the total file size.

the case

show running containers

Use the docker ps command to display the currently running docker containers.

haicoder(www.haicoder.net)# docker ps

After running, the terminal displays information as shown in the figure below:

Please add a picture description

Use the docker run command to start the docker container.

haicoder(www.haicoder.net)# docker run -it -d ubuntu
#输出
2b7be687c268e9651b70ad2d84ac9487482dea0587c9e50471c8fcdeea848a77

Use the docker ps command again to display the currently running docker container, and the terminal display information is as shown in the figure below:

insert image description here

At this point, the list of running containers has information about the container that was just running.

Show the most recently created N containers

Using the docker run command, start five docker containers.

haicoder(www.haicoder.net)# docker run -it -d ubuntu
bfffa738d6d31404af615a18da1883c66e830ac8a4c11a0e93c05123649f1ee0
haicoder(www.haicoder.net)# docker run -it -d ubuntu
075c757f6a04278993a925cd2a1717557a68cd6631008c1445eaa510abf70ad0
haicoder(www.haicoder.net)# docker run -it -d ubuntu
7ab8cab8d002068f97d94a1e315b89844113fd5d47bea94001d1c7c18e3f06a1
haicoder(www.haicoder.net)# docker run -it -d ubuntu
234972c5a5630b1aa53f955ffaa28509cc20cfe5c6f387d1243a0d418a4fd28d
haicoder(www.haicoder.net)# docker run -it -d ubuntu
275dca19d3afd755ad72ccd5666060b828e90a2166954b8623d8887087d992fc

Use the docker ps command to display the currently running docker container, and the terminal display information is as shown in the figure below:

insert image description here

Use the docker ps -n 3 command to display the 3 currently running docker containers, and the terminal display information is as shown in the figure below:

insert image description here

At this point, we see that only the 3 most recently run docker containers are shown, not all of them. That is, in the above case, we use the docker ps -n command to display the recently created N docker containers.

Finally, we use the docker kill and docker rm commands to remove all containers.

haicoder(www.haicoder.net)# docker kill `docker ps -qa` && docker rm `docker ps -qa`
275dca19d3af
234972c5a563
7ab8cab8d002
075c757f6a04
bfffa738d6d3
2b7be687c268
275dca19d3af
234972c5a563
7ab8cab8d002
075c757f6a04
bfffa738d6d3
2b7be687c268

show all containers

Using the docker run command, start three docker containers.

haicoder(www.haicoder.net)# docker run -it -d ubuntu                                
1e4956dd7ef8dca2844a94af218a8f3272d2fbc72df9839e6bb0a5dcca598c65
haicoder(www.haicoder.net)# docker run -it -d ubuntu
bc427c7eb2ad3247dd77198516d2eeda93697861775c6faef48511b534499a5c
haicoder(www.haicoder.net)# docker run -it -d ubuntu
790dd6cfd43e8f552687b5fa4465d81fc49d1ca1af37354c7562618a63364ce0

Use the docker ps command to display the currently running docker container, and the terminal display information is as shown in the figure below:

insert image description here

Use the docker stop command to stop two of the docker containers.

haicoder(www.haicoder.net)# docker stop 790dd6cfd43e bc427c7eb2ad 
790dd6cfd43e
bc427c7eb2ad

Use the docker ps command again to display the currently running docker container, and the terminal display information is as shown in the figure below:
insert image description here

At this point, only one running docker container is displayed, and the two docker containers that we just stopped are not displayed.

We use the docker ps -a command to display all docker containers, whether they are running or not.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-7DHYGk5u-1673927980178) (F:\Hi Ke\Documents\Server\Docker\07 Docker command--operate operation dimension\docker\07 docker ps -a.png)]

At this point, the three containers we created are displayed, one running and two not running. That is, in the above case, we use the docker ps -a command to display all docker containers, including running and not running.

Finally, we use the docker kill and docker rm commands to remove all containers.

haicoder(www.haicoder.net)# docker kill `docker ps -qa` ;  docker rm `docker ps -qa`

Show all container IDs

Using the docker run command, start three docker containers.

haicoder(www.haicoder.net)# docker run -it -d ubuntu 
c8c6f11276b77b57a081b50b28a20b84c28de0e3c0baf4d261c6b9eff07d60d7
haicoder(www.haicoder.net)# docker run -it -d ubuntu 
463bbe5457134a51078297f5cb7ccbc1d10c7d1d870169cf0096712d68bed2e7
haicoder(www.haicoder.net)# docker run -it -d ubuntu 
4af5cf8ed40dec5d23b7cbe85274553030a25939f327614e2cb826e644d58c45

Use the docker ps command to display the currently running docker container, and the terminal display information is as shown in the figure below:

insert image description here

We use the docker ps -aq command to display the ID information of all docker containers, including whether they are running or not. The terminal display is as follows:

insert image description here

At this time, not all the information of the container is displayed, only the ID information of the container is displayed. The ID here can cooperate with commands such as docker kill and docker rm to realize batch management of containers.

That is, in the above case, we use the docker ps -aq command to display the IDs of all docker containers, including running and not running.

Finally, we use the docker kill and docker rm commands to remove all containers.

haicoder(www.haicoder.net)# docker kill `docker ps -qa` ;  docker rm `docker ps -qa`

Display the full form of the container ID

Use the docker run command to start the docker container.

haicoder(www.haicoder.net)# docker run -it -d ubuntu
dfeed15d07dbc8af110c407369b592b366a8501dca900e600467629ab1eeb798

Use the docker ps --no-trunc command to display the complete form of the docker container ID, and the terminal display information is as shown in the figure below:
insert image description here

At this point, the CONTAINER ID column in the returned result shows the full ID of the container. That is, in the above case, we used the docker ps --no-trunc command to display the full form of the docker container ID, not the abbreviated form.

Finally, we use the docker kill and docker rm commands to remove all containers.

haicoder(www.haicoder.net)# docker kill `docker ps -qa` ;  docker rm `docker ps -qa`

show container size

Use the docker run command to start the docker container.

haicoder(www.haicoder.net)# docker run -it -d ubuntu
379e89ce154a5e1a4821eeac352d36506f971c64f8b416ba571a4a02a7e3b45a

Use the docker ps -s command to display the size information of the docker container. The terminal display information is as shown in the following figure:
insert image description here

At this time, there is an additional column SIZE in the returned result, and SIZE has two values, one is the actual increased size of the container, and the other is the virtual size of the entire container. The virtual size of the container = the real increased size of the container + the size of the container image.

That is, in the above case, we use the docker ps -s command to display the size information of the docker container.

Finally, we use the docker kill and docker rm commands to remove all containers.

haicoder(www.haicoder.net)# docker kill `docker ps -qa` ;  docker rm `docker ps -qa`

Advanced usage of docker ps command

If there are too many containers, or you want to exclude interfering containers, you can use docker ps --filter or docker ps -f to filter the containers that need to be displayed.

docker ps filter rules

The options are followed by key-value pairs key=value (without quotation marks). If there are multiple filter conditions, use the filter option multiple times. For example:

haicoder(www.haicoder.net)# docker ps --filter id=a1b2c3 --filter name=haicoder

The relationship between the same conditions is or, and the relationship between different conditions is and relationship. For example:

haicoder(www.haicoder.net)# docker ps --filter name=haicoder --filter name=hicoder --filter status=running

The above filter conditions will find out the container whose name contains haicoder or hicoder and whose status is running.

id and name support regular expressions and are very flexible to use. For example:

haicoder(www.haicoder.net)# docker ps --filter name=^/haicoder$ 

docker ps formatted display

If you want to customize the display of container fields, you can use the format option --format , refer to docker formatting for details.

Result analysis

field name illustrate
CONTAINER ID The container ID means that the unique corresponding container can be found through this id.
IMAGE The image used by this container.
COMMAND The command to run when starting the container.
CREATED The creation time of the container, displayed in the format of "created before ** time".
STATUS The current state of the container.
PORTS The port information of the container and the connection type (tcp\udp) used.
NAMES The name automatically created by the image for the container also uniquely represents a container.

container status

state illustrate
created created.
restarting restarting.
running running.
removing migrating.
paused pause.
exited quit.
dead Sudden death.

Docker ps command summary

The docker ps command can be used to list information about containers.

Docker ps command syntax:

docker ps [OPTIONS]

Guess you like

Origin blog.csdn.net/weixin_41384860/article/details/128714426