Docker basic container basic command operation and detailed explanation, container commands that docker must know

content

docker run creates a new container and starts it:

exit the container

docker ps view container

delete container

Actions to start and stop containers:

into the container:



The following commands are the basic container commands that we must know with our eyes closed when learning docker:

Note: We need to have an image to create a container, here we download a centos image to test and learn.

docker pull centos     #不写标签默认下载最新版镜像

docker run creates a new container and starts it:

docker run [可选参数] 镜像名或ID

#参数说明
--name="Name"    容器名字  tomcat01  tomcat02,用来区分容器
-d    后台方式运行
-it   使用交互方式运行,进入容器查看内容
-p    指定容器的端口 -p 8080:8080

    
    -p 主机端口:容器端口 (常用) 
    -p 容器端口
    -p ip:主机端口:容器端口
     容器端口

-P   随机指定端口     大写的P是随机指定端口,小写的p是手动配置端口


#测试,启动并进入容器:
[root@localhost ~]# docker run -it centos /bin/bash    #以交互式的运行方式启动容器
[root@a2661bc2e2da /]#
    启动成功后,主机名会变成镜像ID
[root@a2661bc2e2da /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
#推出容器命令:exit
[root@a2661bc2e2da /]# exit
exit
[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost ~]#

exit the container

exit        # stop and exit the container

Use the shortcut key Ctrl+p+q        #Do not stop and exit the container

#停止并退出容器
[root@a2661bc2e2da /]# exit
exit

#退出容器,但不停止容器,容器依旧在后台运行
[root@localhost ~]# docker run -it centos /bin/bash
[root@dd5995bd32c8 /]# [root@localhost ~]#        #使用快捷键Ctrl+p+q
[root@localhost ~]#

The directory inside the container and the directory outside the container are isolated from each other and do not affect each other.

docker ps view container

parameter:

Without arguments # list currently running containers

-a #List currently running containers + bring out the containers that have been run in history, which is equivalent to listing all containers

-n=? #Show recently created containers (? means you want to see the recently created containers, just fill in the numbers)

-q #Only display the container number Can be used in combination with -a, -aq to view the number of all containers, the same as the image command usage

[root@localhost ~]# docker ps    #查看正在运行的容器
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@localhost ~]# docker ps -a    #查看所有容器
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
a2661bc2e2da   centos         "/bin/bash"   9 minutes ago   Exited (0) 4 minutes ago             boring_chatelet
654f4f39c05e   feb5d9fea6a5   "/hello"      3 days ago      Exited (0) 3 days ago                compassionate_liskov

[root@localhost ~]# docker ps -n=1    #查看最近创建的一个容器
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                     PORTS     NAMES
a2661bc2e2da   centos    "/bin/bash"   12 minutes ago   Exited (0) 7 minutes ago             boring_chatelet

[root@localhost ~]# docker ps -aq    #查看所有容器的ID编号
a2661bc2e2da
654f4f39c05e

delete container

#删除单个容器:
docker rm -f 容器名或ID

#删除所有容器
docker rm -f $(docker ps -aq)    #将所有的容器id传递给删除命令,递归删除

#第二种删除全部容器的命令:
docker ps -a -q|xargs docker rm   

$()    传递参数给该命令



#测试:
[root@localhost ~]# docker rm -f $(docker ps -aq)
dd5995bd32c8  #删除容器会显示出被删除的容器ID
a2661bc2e2da
654f4f39c05e
[root@localhost ~]#

Actions to start and stop containers:

#使用这些命令的基础是需要创建了镜像,没有镜像不能直接start启动

docker start 容器ID    #启动一个停止的容器
docker restart 容器ID   #重启一个容器
docker stop 容器ID    #停止一个容器
docker kill 容器ID    #暴力停止容器,直接杀死这个容器,强制停止容器
若docker stop报错了 则可以使用docker kill 强制停止容器

根据之前的 $() 符号,可以衍生到这些命令
#启动所有容器
[root@localhost ~]# docker start $(docker ps -aq)
f8f5f28eabee
27833ac6fc62
7f497e338757
#停止所有容器
[root@localhost ~]# docker stop $(docker ps -aq)
f8f5f28eabee
27833ac6fc62
7f497e338757

也可以重启所有容器等等操作
$()符号 很常用,很方便

into the container:

The premise of entering the container is to start the container

#进入容器命令:
docker attach 容器名;ID


#启动一个已经创建好的容器
[root@localhost ~]# docker start f8f5f28eabee
f8f5f28eabee

#进入这个启动的容器
[root@localhost ~]# docker attach f8f5f28eabee
[root@f8f5f28eabee /]#

Guess you like

Origin blog.csdn.net/weixin_53466908/article/details/124203192