Summary of Common Commands for docker

Keywords: Docker CentOS Anaconda

I won't mention the advantages of a specific docker, but here are some common docker commands:

1. Start, stop, restart of docker

[root@localhost ~]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[root@localhost ~]# service docker stop
Redirecting to /bin/systemctl stop docker.service
[root@localhost ~]# service docker start
Redirecting to /bin/systemctl start docker.service

2. docker creates a container

  
[root@localhost ~]# docker run -it -v /docker_test:/yufei  --name yufei_6 centos
[root@724e7701f0d4 /]# 
-i: Allows us to interact with (STDIN) in containers
-t: Specify a pseudo terminal or terminal in a new container
-v: is hanging in the hosting directory, /docker_The test is the hosting directory, /yufei is the directory of the current docker container, and the hosting directory must be absolute.
--name: is to give the container a name, omitted, docker will randomly produce a name if omitted

3. List of containers started by docker

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
724e7701f0d4        centos              "/bin/bash"         4 minutes ago       Up 4 minutes                            yufei_6
f9097691663e        centos              "/bin/bash"         5 minutes ago       Up 5 minutes                            yufei_5
[root@localhost ~]# 

3. View all containers created by docker

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
724e7701f0d4        centos              "/bin/bash"         5 minutes ago       Up 5 minutes                                      yufei_6
f9097691663e        centos              "/bin/bash"         6 minutes ago       Up 6 minutes                                      yufei_5
e59a540fb979        centos              "/bin/base"         6 minutes ago       Created                                           yufei_4
ff49dfedea4f        centos              "/bin/bash"         2 hours ago         Exited (137) 10 minutes ago                       yufei_03
d2cc70abb5a5        centos              "/bin/bash"         2 hours ago         Exited (127) 2 hours ago                          yufei_02
2d48fc5b7c17        centos              "/bin/bash"         2 hours ago         Exited (127) 2 hours ago                          yufei_01
[root@localhost ~]# 
The docker ps default list is the launching container-a is the display of all created containers

4. Start, stop, restart a docker container

[root@localhost ~]# docker start yufei_01
yufei_01
[root@localhost ~]# docker stop yufei_01
yufei_01
[root@localhost ~]# docker restart yufei_01
yufei_01
[root@localhost ~]# 

5. View log records for specified containers

[root@localhost ~]# docker logs -f yufei_01
[root@2d48fc5b7c17 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@2d48fc5b7c17 /]# exit
exit
[root@2d48fc5b7c17 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@2d48fc5b7c17 /]# 
[root@2d48fc5b7c17 /]# 
[root@2d48fc5b7c17 /]# 
[root@2d48fc5b7c17 /]# 
[root@2d48fc5b7c17 /]# 
[root@2d48fc5b7c17 /]# 
[root@2d48fc5b7c17 /]# 
[root@2d48fc5b7c17 /]# cd / 
[root@2d48fc5b7c17 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@2d48fc5b7c17 /]# mkdir yufei
[root@2d48fc5b7c17 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  yufei
[root@2d48fc5b7c17 /]# cd yufei
[root@2d48fc5b7c17 yufei]# ls
yufei
[root@2d48fc5b7c17 yufei]# cd yufei
[root@2d48fc5b7c17 yufei]# ls
application
[root@2d48fc5b7c17 yufei]# cd ../../
[root@2d48fc5b7c17 /]# rm -rf yufei
[root@2d48fc5b7c17 /]# eixt
bash: eixt: command not found
[root@2d48fc5b7c17 /]# exit
exit
[root@2d48fc5b7c17 /]# 

The red part above is the log command part.

6. Delete a container and stop it if it is running

[root@localhost ~]# docker rm yufei_01
Error response from daemon: You cannot remove a running container 2d48fc5b7c17b01e6247cbc012013306faf1e54f24651d5e16d6db4e15f92d33. Stop the container before attempting removal or use -f
[root@localhost ~]# docker stop yufei_01
yufei_01
[root@localhost ~]# docker rm yufei_01
yufei_01
[root@localhost ~]# 

7. Delete all containers

[root@localhost ~]# docker rm $(docker ps -a -q)
Error response from daemon: You cannot remove a running container 724e7701f0d4a830167e21f75b470235a0e408fd6cc2913403426e96f69cba11. Stop the container before attempting removal or use -f
Error response from daemon: You cannot remove a running container f9097691663ee36f9d2ee56afbdcca0eeb8b63e5590ddf18c0c42954c93b9f06. Stop the container before attempting removal or use -f
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# docker stop yufei_6
yufei_6
[root@localhost ~]# docker stop yufei_5
yufei_5
[root@localhost ~]# docker rm $(docker ps -a -q)
724e7701f0d4
f9097691663e
[root@localhost ~]# 


Posted by nicob on Sun, 05 Jul 2020 08:30:50 -0700