Docker常用的命令【基础命令】

本文介绍了Docker的常用命令,包括查看版本信息、系统信息、搜索镜像、下载镜像、删除镜像、管理容器(如新建、启动、停止、删除)、查看日志、进入容器、拷贝文件等操作,详细解析了每个命令的使用方法和参数。
摘要由CSDN通过智能技术生成

Docker的常用命令

帮助命令

docker version #显示docker的版本信息
docker info    #显示docker的系统信息,包括镜像和容器的数量
docker  命令 --help #查看对应命令的用法

帮助文档地址:https://docs.docker.com/engine/reference/commandline/images/

镜像命令

docker images #查看所有本地上的镜像

[root@slave2 liu]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   8 months ago   13.3k
#解释
REPOSITORY  镜像的仓库源
TAG			镜像的便签
IMAGE ID	镜像的ID
CREATED		镜像的创建时间
SIZE		镜像的大小
#可选项
  -a, --all             #显示全部的镜像
      --digests         #显示细节
  -q, --quiet          #只显示镜像的ID

docker search #搜索镜像

[root@slave2 liu]# docker search mysql
NAME                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                          MySQL is a widely used, open-source relation…   12614     [OK]       
mariadb                        MariaDB Server is a high performing open sou4844      [OK]       
#可选项
  -f, --filter filter   #通过搜索来过滤,--filter=STARS=3000,搜索的镜像的STARS大于3000--format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output

**docker pull 镜像名[tag] **下载镜像

[root@slave2 liu]# docker pull mysql 
Using default tag: latest #如果不写tag,默认就是latest
latest: Pulling from library/mysql
c32ce6654453: Pull complete  #分层下载,docker image的核心,联合文件系统
415d08ee031a: Pull complete 
7a38fec2542f: Pull complete 
352881ee8fe9: Pull complete 
b8e20da291b6: Pull complete 
66c2a8cc1999: Pull complete 
d3a3a8e49878: Pull complete 
e33a48832bec: Pull complete 
410b942b8b28: Pull complete 
d5323c9dd265: Pull complete 
0fb8320c65f1: Pull complete 
bd2a54f30bfc: Pull complete 
Digest: sha256:844132a773021fc62303ba1de52f15758fbdd1201bfc7b1c3e09cf6293a4ff53
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest  #真实地址
docker pull mysql ==dockerpull  docker.io/library/mysql:latest

#指定版本号下载
[root@slave2 liu]# docker pull mysql:5.7
5.7: Pulling from library/mysql
c32ce6654453: Already exists 
415d08ee031a: Already exists 
7a38fec2542f: Already exists 
352881ee8fe9: Already exists 
b8e20da291b6: Already exists 
66c2a8cc1999: Already exists 
d3a3a8e49878: Already exists 
172aabfba65c: Pull complete 
fea17d0b1d1e: Pull complete 
5ba59babc3fd: Pull complete 
e31ade51bfbf: Pull complete 
Digest: sha256:17c173d46409fb0527912d9635be0a31d4fb087af8d00e5102ff7c417f987fa8
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

**docker rmi **删除镜像

docker rmi -f 镜像id #删除指定的容器
docker rmi -f 镜像id 镜像id 镜像id #删除大量的容器
docker rmi -f $(docker image -aq) #删除全部的容器

容器命令

新建容器并启动

docker run [可选参数] image
#参数说明
--name="name"   容器名字 
-d 				后台方式运行
-it				使用交互方式,进入容器查看内容
-p				指定容器的端口 -p 8080:8080
	-p 主机端口:容器端口【常用】
	-p 容器端口
	-p ip:主机端口:容器端口
-p   随机指定端口

#测试,启动并进入容器
docker run -it centos /bin/bash
[root@slave2 mytest]# docker run -it centos /bin/bash
[root@3564c61cc662 /]# ls -al
total 0
drwxr-xr-x   1 root root   6 May 22 07:55 .
drwxr-xr-x   1 root root   6 May 22 07:55 ..
-rwxr-xr-x   1 root root   0 May 22 07:55 .dockerenv
lrwxrwxrwx   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root 360 May 22 07:55 dev
drwxr-xr-x   1 root root  66 May 22 07:55 etc
drwxr-xr-x   2 root root   6 Nov  3  2020 home
lrwxrwxrwx   1 root root   7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root   9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root   6 Sep 15  2021 lost+found
drwxr-xr-x   2 root root   6 Nov  3  2020 media
drwxr-xr-x   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x 210 root root   0 May 22 07:55 proc
dr-xr-x---   2 root root 162 Sep 15  2021 root
drwxr-xr-x  11 root root 163 Sep 15  2021 run
lrwxrwxrwx   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x  13 root root   0 May 22 07:55 sys
drwxrwxrwt   7 root root 171 Sep 15  2021 tmp
drwxr-xr-x  12 root root 144 Sep 15  2021 usr
drwxr-xr-x  20 root root 262 Sep 15  2021 var
#从容器中退出,回到主机
exit

列出所有的运行的容器

#docker ps 命令
-a #列出当前运行的容器和历史运行的容器
-n=? #显示最近创建的容器
-q   #只显示容器的编号

[root@slave2 mytest]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
3564c61cc662   centos    "/bin/bash"   About a minute ago   Up About a minute             tender_bouman
[root@slave2 mytest]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED         STATUS                   PORTS     NAMES
3564c61cc662   centos        "/bin/bash"   2 minutes ago   Up 2 minutes                       tender_bouman
da14bc6c7f2d   hello-world   "/hello"      6 hours ago     Exited (0) 6 hours ago             recursing_payne
233c9b6503a6   hello-world   "/hello"      6 hours ago     Created                            magical_faraday
[root@slave2 mytest]# 

退出容器

 exit 	    #退出容器并停止容器
 Ctrl+P+Q   #容器不停止,退出

删除容器

docker rm 容器id   		#删除指定的容器
docker rm -f $(docker ps -aq) #删除全部的容器
docker ps -a -q|xargs docker rm #删除所有的容器

启动和停止容器的操作

docker start 容器id 	#启动容器
docker restart 容器id  #重新启动容器
docker stop 容器id	#停止容器
docker kill 容器id 	#杀死容器

常用的其他命令

后台启动容器

#docker run -d 镜像名
[root@slave2 mytest]# docker run -d centos
8938a339797de5c856bfc72bafe6fd6212f796b2a1c684f74a6dae15554c3b45
#docker ps 发现此centos停止了
docker的容器使用后台运行时,就必须要有一个前台进程,如果此时docker发现该容器没有应用,就会自动停止
在容器启动后,容器发现自己没有提供服务,就会立刻停止服务,也就没有程序了

查看日志

docker logs
#测试
#while true; do echo liupeihengchenyanzhen;sleep 5;done
#这段话的意思是,执行一个死循环,每5秒打印liupeihengchenyanzhen
[root@slave2 mytest]# docker run -d centos /bin/sh -c "while true; do echo liupeihengchenyanzhen;sleep 5;done"
6cf154595e377e8dab37dbee116c4c54a3cca8e461a13d0a2f3c6bf2b44a490f
[root@slave2 mytest]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
6cf154595e37   centos    "/bin/sh -c 'while t…"   5 seconds ago   Up 4 seconds             focused_proskuriakova
#查看日志,只查看10行信息
[root@slave2 mytest]# docker logs -tf --tail 10 6cf154595e37
2022-05-22T08:22:45.079558530Z liupeihengchenyanzhen
2022-05-22T08:22:50.102797299Z liupeihengchenyanzhen

#参数可选
-tf 				 #显示日志
-n, --tail number    显示日志的条数

查看容器中的进程信息

docker top 容器id

查看镜像源数据

docker inspect 容器id
[root@slave2 mytest]# docker inspect c354cbb5f09a
[
    {	#容器的主ID
        "Id": "c354cbb5f09a552003687fad9d76f3dbb51e46959508c91b9b75092633b179a9",
        #容器的建立时间
        "Created": "2022-05-22T08:32:58.792263193Z",
        "Path": "/bin/sh",
        #传入的数据
        "Args": [
            "-c",
            "while true; do echo liupeihengchenyanzhen;sleep 5;done"
        ],
        #容器当前的状态
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 49645,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-05-22T08:32:59.85460653Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        #容器的配置
        "Config": {
            "Hostname": "c354cbb5f09a",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            #这是它里面的环境变量配置
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            #这是传进来的shell脚本
            "Cmd": [
                "/bin/sh",
                "-c",
                "while true; do echo liupeihengchenyanzhen;sleep 5;done"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
       		#网络信息
            "Networks": {
            #采用桥接模式
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "301eecfca11afb81e673f54fdc64ab339add6e4b22572b24348cb84407a5abe3",
                    "EndpointID": "ddefef376ef1246ecb80993a3557d72f772fcbffb972403d3fa9c3915c3f8193",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

进入当前正在运行的容器

docker exec -id 容器id bashShell
#测试
[root@slave2 mytest]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
c354cbb5f09a   centos    "/bin/sh -c 'while t…"   7 minutes ago   Up 7 minutes             zealous_bardeen
[root@slave2 mytest]# docker exec -it c354cbb5f09a /bin/bash
[root@c354cbb5f09a /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@c354cbb5f09a /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 08:32 ?        00:00:00 /bin/sh -c while true; do echo liupeihengcheny
root        100      0  0 08:40 pts/0    00:00:00 /bin/bash
root        116      1  0 08:40 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sl
root        117    100  0 08:40 pts/0    00:00:00 ps -ef


#进入的方式二:
docker attach 容器id 

#两者区别
docker exec  #进入容器后开启一个新的终端,可以在里面进行操作(常用)
docker attach #进入容器正在执行的终端,不会启动新的进程

从容器内拷贝文件到主机上

docker cp 容器id:容器内路径 目的主机路径

测试

#进入docker容器内部
[root@slave2 mytest]# docker attach 563ac0e05973
[root@563ac0e05973 /]# cd home
#新建文件
[root@563ac0e05973 home]# touch test.java
[root@563ac0e05973 home]# ls -al
total 0
drwxr-xr-x 1 root root 23 May 22 08:58 .
drwxr-xr-x 1 root root 18 May 22 08:55 ..
-rw-r--r-- 1 root root  0 May 22 08:58 test.java
[root@563ac0e05973 home]# exit
exit
[root@slave2 mytest]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS                        PORTS     NAMES
563ac0e05973   centos        "/bin/bash"              3 minutes ago       Exited (0) 9 seconds ago                gracious_babbage
#把容器的文件复制到主机上
[root@slave2 mytest]# docker cp 563ac0e05973:/home/test.java /home/liu/mytest
[root@slave2 mytest]# ls -al
总用量 4
drwxr-xr-x   2 root root   23 522 16:58 .
drwx------. 23 liu  liu  4096 522 15:40 ..
-rw-r--r--   1 root root    0 522 16:58 test.java

拷贝是一个手动的过程,未来我们使用数据卷【-v】 的方式进行自动复制

小结

  attach      #当前shell下 attach连接指定运行的镜像
  build       # 通过Dockerfile定制镜像
  commit      #提交当前容器为新的镜像
  cp          #拷贝文件
  create      #创建一个新的容器
  diff        #查看docker容器的变化
  events      # 从服务获取容器实时时间
  exec        # 在运行中的容器上运行命令
  export      #导出容器文件系统作为一个tar归档文件[对应import]
  history     # 展示一个镜像形成历史
  images      #列出系统当前的镜像
  import      #从tar包中导入内容创建一个文件系统镜像
  info        # 显示全系统信息
  inspect     #查看容器详细信息
  kill        # kill指定docker容器
  load        #从一个tar包或标准输入中加载一个镜像[对应save]
  login       #注册或者登录一个docker源服务器
  logout      #从当前的docker仓库退出
  logs        #输出当前容器的日志信息
  port        #查看映射端口对应的容器内部源端口
  ps          #列出容器的列表
  pull        #从docker镜像源服务器拉去指定的镜像或者库镜像到本地
  push        #推送指定镜像或者库镜像到docker源服务器
  restart     #重启运行的容器
  rm          #移除一个或多个容器
  rmi         #移除一个或者多个镜像
  run         #创建一个新的容器并运行一个命令
  save        #保存一个镜像为tar包【对应load】
  search      #在docker hub上搜索镜像
  start       #启动容器
  stats       #容器的状态
  stop        #停止容器运行
  tag         #给源镜像打标签
  top         #查看容器中运行的进程信息
  unpause     #取消暂停容器
  version     #查看docker版本号
  wait        #截取容器停止时的退出状态值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值